sort和priority_queue的比较函数总结
对于priority_queue来说,,比较函数为(如果不是结构体,直接int,优先队列默认的是值越大优先级越大):
struct st
{
string str;
int pr, value,mark ;
bool operator < (const st&a)const
{
if(pr !=a.pr) return pr > a.pr;//值小优先级越大
return mark > a.mark;//值越小优先级大
}
};而对于sort来说
1 #include<iostream>
2 #include<algorithm>
3 using namespace std;
4 int cmp(int a, int b)
5 {
6 return a > b;//从大到小排序
7 }
8 int main()
9 {
10 int a[] = { 2,1,4,3,4,5,1,3 };
11 sort(a, a + 8, cmp);
12 for (int i = 0; i < 8; i++)
13 cout << a[i] << " ";
14 return 0;
15 }greater<int>()//从大到小
less<int>()//从小到大
相关推荐
muzirigel 2020-01-18
doupoo 2020-01-18
xiaohong000 2019-04-03
Phplayers 2016-04-02
puddingpp 2019-06-28
怕什么真理无穷 2019-06-21
VermillionTear 2019-06-13
鹤之淇水 2019-03-08
yawei 2018-08-16
chuzizuo 2013-04-08
贝伦酱 2009-12-07
YourDreamStarted 2015-12-11
wangschang 2018-05-16
红薯藤 2019-04-16
wangtengphp 2019-04-16
phpcrazy 2019-04-16
weixuejunphp 2016-01-20