#include #include #include using namespace std; bool cmp(const int& a, const int& b) { return a < b; } class compare { public: template bool operator()(const T& a, const T& b) { //返回大的,泛型不能直接返回指针,没有初始化 //用struct或者class套一下,在class初始化的过程中就顺带着初始化了 return a < b;//false是向上swap } }; int main() { auto cmpl = [](const T& a, const T& b) { return a < b; }; //priority_queue, decltype(cmpl)> pq(cmpl);合法 //priority_queue, bool(*)(const int&, const int&)> pq(cmp);合法 priority_queue, compare> pq; //上面三种写法非常重要 pq.push(1); pq.push(2); pq.push(6); pq.push(3); pq.push(7); pq.push(4); while (!pq.empty()) { cout << pq.top() << " "; pq.pop(); } cout << endl; return 0; } /* 7 6 4 3 2 1 */