fork download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <functional>
  5. #include <queue>
  6. #include <cmath>
  7.  
  8. class Test {
  9. public:
  10. Test(int reference) :
  11. m_reference( reference ),
  12. m_priority(
  13. [ref=reference]( int a, int b ) {
  14. return std::abs( a - ref ) < std::abs( b - ref );
  15. } )
  16. { }
  17. void feed(int x) { m_priority.push(x); }
  18. int get() { return m_priority.top(); }
  19. private:
  20. int m_reference;
  21. std::priority_queue<int, std::vector<int>, std::function<bool(int,int)>> m_priority;
  22. };
  23.  
  24. int main() {
  25. Test test(13);
  26. test.feed(1);
  27. test.feed(10);
  28. test.feed(20);
  29.  
  30. auto x = test.get();
  31.  
  32. std::cout << x << std::endl;
  33. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
1