C++中的sort函数默认是将元素升序排列的,而priority_queue默认是将元素降序排列的(默认实现的是大顶堆)。
自定义运算符用的比较多,以下2种对sort和priority_queue运算符的重载都有效,效果都是一样的:
1 #include <iostream> 2 #include <vector> 3 #include <algorithm> 4 #include <queue> 5 using namespace std; 6 7 //sort实现的都是先按a值降序排列,a相同时,按b值降序排列 8 //priority_queue和sort的默认排序相反,实现的都是先按a值升序排列,a相同时,按b值升序排列 9 10 //重载方式一: 11 struct Node { 12 int a, b; 13 Node(int x, int y) { 14 a = x; 15 b = y; 16 } 17 bool operator < (const Node &c) const { 18 if(a == c.a) return b > c.b; 19 return a > c.a; 20 } 21 }; 22 23 //重载方式二:注意要有public 24 // class Node { 25 // public: 26 // int a, b; 27 // Node(int x, int y) { 28 // a = x; 29 // b = y; 30 // } 31 // bool operator < (const Node &c) const { 32 // if(a == c.a) return b > c.b; 33 // return a > c.a; 34 // } 35 // }; 36 int main() { 37 vector<Node> v; 38 v.push_back(Node(1,2)); 39 v.push_back(Node(1,4)); 40 v.push_back(Node(4,2)); 41 v.push_back(Node(3,3)); 42 sort(v.begin(), v.end()); 43 for(int i = 0; i < v.size(); ++i) { 44 cout<<v[i].a<<" "<<v[i].b<<endl; 45 } 46 cout<<endl; 47 48 priority_queue<Node> pq; 49 pq.push(Node(1,2)); 50 pq.push(Node(1,4)); 51 pq.push(Node(4,2)); 52 pq.push(Node(3,3)); 53 while(!pq.empty()) { 54 cout<<pq.top().a<<" "<<pq.top().b<<endl; 55 pq.pop(); 56 } 57 return 0; 58 }
输出
4 2 3 3 1 4 1 2 1 2 1 4 3 3 4 2
对sort单独自定义运算符:
1 #include <iostream> 2 #include <vector> 3 #include <algorithm> 4 #include <queue> 5 using namespace std; 6 7 //sort实现的都是先按a值降序排列,a相同时,按b值降序排列 8 //priority_queue和sort的默认排序相反,实现的都是先按a值升序排列,a相同时,按b值升序排列 9 10 struct Node { 11 int a, b; 12 Node(int x, int y) { 13 a = x; 14 b = y; 15 } 16 }; 17 /* 18 //重载方式一: 19 struct compare { 20 bool operator()(const Node a, const Node b) { 21 if(a.a == b.a) return a.b > b.b; 22 return a.a > b.a; 23 } 24 } cmp; 25 */ 26 27 /* 28 //重载方式二: 29 bool cmp(const Node a, const Node b) { 30 if(a.a == b.a) return a.b > b.b; 31 return a.a > b.a; 32 } 33 */ 34 35 int main() { 36 37 vector<Node> v; 38 v.push_back(Node(1,2)); 39 v.push_back(Node(1,4)); 40 v.push_back(Node(4,2)); 41 v.push_back(Node(3,3)); 42 //重载方式一和二: 43 sort(v.begin(), v.end(), cmp); 44 //重载方式三: 45 sort(v.begin(), v.end(), [](const Node a, const Node b) { 46 if(a.a == b.a) return a.b > b.b; 47 return a.a > b.a; 48 }); 49 for(int i = 0; i < v.size(); ++i) { 50 cout<<v[i].a<<" "<<v[i].b<<endl; 51 } 52 cout<<endl; 53 return 0; 54 }
输出都是
4 2 3 3 1 4 1 2
对priority_queue单独自定义运算符:
#include <iostream> #include <vector> #include <algorithm> #include <queue> using namespace std; //sort实现的都是先按a值降序排列,a相同时,按b值降序排列 //priority_queue和sort的默认排序相反,实现的都是先按a值升序排列,a相同时,按b值升序排列 struct Node { int a, b; Node(int x, int y) { a = x; b = y; } }; struct cmp { bool operator()(const Node a, const Node b) { if(a.a == b.a) return a.b > b.b; return a.a > b.a; } }; int main() { priority_queue<Node, vector<Node>, cmp> pq; pq.push(Node(1,2)); pq.push(Node(1,4)); pq.push(Node(4,2)); pq.push(Node(3,3)); while(!pq.empty()) { cout<<pq.top().a<<" "<<pq.top().b<<endl; pq.pop(); } return 0; }
效果:
1 2 1 4 3 3 4 2