STL数据结构

  • priority_queue

  "C++ reference"上如此解释priority queue:"This context is similar to a heap, where elements can be inserted at any moment, and only the max heap element can be retrieved (the one at the top in the priority queue)."

  这里用个小例子使用下priority_queue:

#include <queue>
#include <iostream>
#include <vector>
#include <functional>

using namespace std;

class comparison{
public:
  bool operator() (const int& lhs, const int&rhs) const
  {
    return lhs < rhs;
  }
};

typedef priority_queue<int,vector<int>,comparison> mycompare;

int main(){

  vector<int> v({2,4,1,12,6,23,4});
  
  //几种申明方式
  priority_queue<int> first;
  priority_queue<int> second (myints,myints+4);
  priority_queue<int, std::vector<int>, std::greater<int> >
                            third (myints,myints+4);

  mycompare prq(v.begin(), v.end());

  while(!prq.empty()){
        cout << prq.top() << endl;
        prq.pop();
  }

  return 0;
}
原文地址:https://www.cnblogs.com/JimmyTY/p/5563064.html