stl中的各種容器的比較器
阿新 • • 發佈:2019-01-13
#include <iostream> #include <queue> #include <map> #include <set> using namespace std; struct cmp1 { bool operator()(int a,int b) { if (a<b) { return true; //此時是大根堆 若想建小根堆 則改為a>b } else { return false; } } }; bool cmp(int a, int b) { if (a < b) { return true; } else { return false; } } int main() { priority_queue<int, vector<int>, cmp1>que; //此時是大根堆 set<int, cmp1>s; //按key值遞增順序 map<int,int,cmp1>m; //按key值遞增順序 vector<int>v; sort(v.begin(),v.end(),cmp);//遞增排序 return 0; }
note:優先順序佇列也就是堆初始化時必須要多加一個引數,priority_queue<int, vector, cmp1>que,cmp1為仿函式,堆還有一點和其他容器不同的是a<b時為大根堆。
vector容器初始化時不能加入任何比較器,只能呼叫sort時加入比較器。