c++ sort 和 priority_queue中的自定義排序
阿新 • • 發佈:2020-11-30
sort()
宣告
template <class RandomAccessIterator, class Compare> void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);
示例
1 // sort algorithm example 2 #include <iostream> 3 #include <algorithm> 4 #include <vector> 5 #include <string> 67 8 using namespace std; 9 10 // 定義結構體 11 struct student 12 { 13 int age; 14 string name; 15 student(int a, string s) 16 { 17 age = a; 18 name = s; 19 } 20 }; 21 22 23 // 自定義比較函式 24 bool myfunction(int i, int j) { 25 return (i < j); 26 } 27 28 29 //過載運算子 注意引數型別要求30 bool operator<(const student s1, const student s2) 31 { 32 if (s1.age == s2.age) 33 return s1.name < s2.name;//年齡相同時,按姓名小到大排 34 else 35 return s1.age < s2.age; //從年齡小到大排序 36 } 37 38 39 // 宣告比較類 40 struct cmp 41 { 42 bool operator() (const student& s1, const student& s2)43 { 44 if (s1.age == s2.age) 45 return s1.name < s2.name; 46 else return s1.age < s2.age; 47 } 48 }mycmp; 49 50 struct cmp2 51 { 52 bool operator() (int a, int b) 53 { 54 return a < b; 55 } 56 }mycmp2; 57 58 59 int main() { 60 vector<int> vec = { 32,71,12,45,26,80,53,33 }; 61 62 63 // 使用預設比較器 (operator <): 64 sort(vec.begin(), vec.end()); //12 26 32 33 45 53 71 80 65 66 // 自定義比較函式(函式指標) 67 sort(vec.begin() + 4, vec.end(), myfunction); // 12 32 45 71(26 33 53 80) 68 69 // 過載比較運算子 70 student s1(10, "wangwu"); 71 student s2(10, "lisi"); 72 student s3(12, "zhangsan"); 73 vector<student> s = { s1, s2, s3 }; 74 //sort(s.begin(), s.end()); //s2 s1 s3 75 76 77 // 宣告比較類 78 // 注意二者寫法的區別 79 sort(s.begin(), s.end(), cmp()); //s2 s1 s3 80 sort(s.begin(), s.end(), mycmp); //s2 s1 s3 81 82 sort(vec.begin(), vec.end(), cmp2()); //12 26 32 33 45 53 71 80 83 sort(vec.begin(), vec.end(), mycmp2); //12 26 32 33 45 53 71 80 84 85 // 使用匿名函式 86 sort(vec.begin(), vec.end(), [](int a, int b) {return a < b; }); //12 26 32 33 45 53 71 80 87 88 system("pause"); 89 return 0; 90 }
priority_queue
宣告
template <class T, class Container = vector<T>, class Compare = less<typename Container::value_type> > class priority_queue;
示例
1 #include<iostream> 2 #include<queue> 3 #include<vector> 4 #include <functional> 5 6 using namespace std; 7 8 template<typename T> void print_queue(T& q) { 9 while (!q.empty()) { 10 cout << q.top() << " "; 11 q.pop(); 12 } 13 cout << endl;; 14 } 15 16 // 宣告比較類 17 struct comp 18 { 19 bool operator()(int in1, int in2) const 20 { 21 return (in1>in2); 22 } 23 }; 24 25 26 int main() 27 { 28 vector<int> v = { 1,3,5,7,9,2,4,6,8,10 }; 29 priority_queue<int> q1; 30 for (auto c : v) 31 { 32 q1.push(c); 33 } 34 print_queue(q1); 35 36 priority_queue<int, vector<int>, greater<int> > q2; 37 for (auto c : v) 38 { 39 q2.push(c); 40 } 41 print_queue(q2); 42 43 // 使用匿名函式 44 auto cmp = [](int a, int b) { return a < b; }; 45 priority_queue<int, vector<int>, decltype(cmp)> q3(cmp); 46 for (auto c : v) 47 { 48 q3.push(c); 49 } 50 print_queue(q3); 51 52 int a = 4; 53 54 // 宣告比較類 55 priority_queue<int, vector<int>, comp> q4; 56 for (auto c : v) 57 { 58 q4.push(c); 59 } 60 print_queue(q4); 61 62 63 cout << "\n" << endl; 64 system("pause"); 65 return 0; 66 }