summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Simianer <p@simianer.de>2015-05-28 21:19:11 +0200
committerPatrick Simianer <p@simianer.de>2015-05-28 21:19:11 +0200
commitd023685532180142747b9d07c15d31bfc56cd436 (patch)
tree920ead84a7f89f28a28f45f73fbfd7466545e14b
parent4a533bed131d68edbbc193263c4abe2a0d7257f6 (diff)
sort2
-rw-r--r--c,cc/sort2.cc62
1 files changed, 62 insertions, 0 deletions
diff --git a/c,cc/sort2.cc b/c,cc/sort2.cc
new file mode 100644
index 0000000..3d64952
--- /dev/null
+++ b/c,cc/sort2.cc
@@ -0,0 +1,62 @@
+#include <iostream>
+#include <algorithm>
+#include <utility>
+#include <vector>
+
+using namespace std;
+
+
+struct X
+{
+ string n;
+ int a;
+ int b;
+};
+
+bool cmph(X a, X b)
+{
+ return (a.a+a.b) > (b.a+b.b);
+}
+
+bool cmpf(X a, X b)
+{
+ return (a.a-a.b) > (b.a-b.b);
+}
+
+int main(void)
+{
+ X a;
+ a.n = "a";
+ a.a = 1;
+ a.b = 2;
+
+ X b;
+ b.n = "b";
+ b.a = 2;
+ b.b = 0;
+
+ X c;
+ c.n = "c";
+ c.a = 4;
+ c.b = -1;
+
+ vector<X> v;
+ v.push_back(a);
+ v.push_back(b);
+ v.push_back(c);
+
+ for (unsigned i = 0; i < v.size(); i++) {
+ cout << v[i].n << " " << v[i].a << " " << v[i].b << endl;
+ }
+ sort(v.begin(), v.end(), cmph);
+ cout << endl;
+ for (unsigned i = 0; i < v.size(); i++) {
+ cout << v[i].n << " " << v[i].a << " " << v[i].b << endl;
+ }
+ sort(v.begin(), v.end(), cmpf);
+ cout << endl;
+ for (unsigned i = 0; i < v.size(); i++) {
+ cout << v[i].n << " " << v[i].a << " " << v[i].b << endl;
+ }
+}
+