fork download
  1. // Example program
  2. #include <iostream>
  3. #include <queue>
  4.  
  5. class Foo
  6. {
  7. public:
  8. Foo(int _a, int _b) : a{_a}, b{_b} {}
  9.  
  10. bool operator<(const Foo& other) const
  11. {
  12. return a < other.a || (a == other.a && b < other.b);
  13. }
  14.  
  15. int GetA() const { return a; }
  16. int GetB() const { return b; }
  17. private:
  18. int a;
  19. int b;
  20. };
  21.  
  22. int main()
  23. {
  24. std::priority_queue<Foo> x;
  25. x.emplace(3,4);
  26. x.emplace(4,1);
  27. x.emplace(5,2);
  28. x.emplace(2,7);
  29.  
  30. while (!x.empty())
  31. {
  32. Foo foo = x.top();
  33. std::cout << "a: " << foo.GetA() << " b: " << foo.GetB() << std::endl;
  34. x.pop();
  35. }
  36. }
  37.  
Success #stdin #stdout 0s 3232KB
stdin
Standard input is empty
stdout
a: 5 b: 2
a: 4 b: 1
a: 3 b: 4
a: 2 b: 7