luisoncpp

priority_queue custom compare

Apr 3rd, 2021
486
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.40 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. #include <vector>
  4.  
  5. int main() {
  6.   auto cmp = [](int a, int b) {
  7.     return a > b;
  8.   };
  9.   std::priority_queue<int, std::vector<int>, decltype(cmp)> Q(cmp);
  10.   for(int a : {5, 3, 10, 2, 11}) {
  11.     Q.push(a);
  12.     std::cout << "TOP: " << Q.top() << std::endl;
  13.   }
  14.   while(!Q.empty()) {
  15.     std::cout << Q.top() << std::endl;
  16.     Q.pop();
  17.   }
  18.   return 0;
  19. }
Advertisement
Add Comment
Please, Sign In to add comment