From d023685532180142747b9d07c15d31bfc56cd436 Mon Sep 17 00:00:00 2001 From: Patrick Simianer Date: Thu, 28 May 2015 21:19:11 +0200 Subject: sort2 --- c,cc/sort2.cc | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 c,cc/sort2.cc (limited to 'c,cc') 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 +#include +#include +#include + +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 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; + } +} + -- cgit v1.2.3