summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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;
+ }
+}
+