Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.97 KB | None | 0 0
  1. #include <iostream>
  2. #include <set>
  3. #include <vector>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8.     struct Foo {
  9.         int a, b, c;
  10.         Foo(int a, int b, int c) : a(a), b(b), c(c) {}
  11.         bool operator<(const Foo& rhs) const {
  12.             return a < rhs.a;
  13.         }
  14.         friend ostream& operator<<(ostream&, const Foo&);
  15.     };
  16.  
  17. ostream& operator<<(ostream& stream, const Foo& foo) {
  18.     stream << foo.a << ", " << foo.b << ", " << foo.c;
  19.     return stream;
  20. }
  21.  
  22. int main() {
  23.     set<Foo> s;
  24.     s.insert({ 1,2,3 });
  25.     s.insert({ 1,3,4 });
  26.     s.insert({ 2,3,1 });
  27.     s.insert({ 2,4,6 });
  28.     s.insert({ 3,5,2 });
  29.     s.insert({ 1,5,7 });
  30.  
  31.     for (auto x : s) {
  32.         cout << x << endl;
  33.     }
  34.    
  35.     vector<Foo> v;
  36.     std::copy(s.begin(), s.end(), std::back_inserter(v));
  37.     std::sort (v.begin(), v.end(), [](const Foo& lhs, const Foo& rhs){ return (lhs.b == rhs.b) ? lhs.c > rhs.c : lhs.b > rhs.b; });
  38.  
  39.     for (auto x : v) {
  40.         cout << x << endl;
  41.     }
  42.  
  43.  
  44.     return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement