首页 > 技术文章 > c++ sort 和 priority_queue中的自定义排序

r1-12king 2020-11-30 16:17 原文

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>
 6 
 7 
 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 }

 

推荐阅读