c++自定义sort函数
来源:互联网 发布:知乎机构号注册流程 编辑:程序博客网 时间:2024/05/16 17:15
头文件:#include <algorithm>
Sort函数使用模板:
Sort(start,end,排序方法) start:起始地址 end:终止地址(亦可传入迭代器起始终止)
sort函数没有第三个参数,默认实现的是从小到大的排序
例题:为成绩排序,定义两科成绩a,b,将成绩按照成绩a从大到小排列,如果成绩a相等,则相等成绩再按照b从大到小排列
#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef struct
{
int a;
int b;
}A;
vector<A> num;
bool cmp(const A &m,const A &n)
{
if(m.a!=n.a)
{
return m.a>n.a;
}else return m.b>n.b;
}
int main()
{
int i,n;
A temp;
cin>>n;
for(i =0; i < n; i++)
{
cin>>temp.a>>temp.b;
num.push_back(temp);
}
sort(num.begin(),num.end(),cmp);
for(i=0;i<n;i++)
{
cout<<num[i].a<<" "<<num[i].b<<endl;
}
return 0;
}
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef struct
{
int a;
int b;
}A;
vector<A> num;
bool cmp(const A &m,const A &n)
{
if(m.a!=n.a)
{
return m.a>n.a;
}else return m.b>n.b;
}
int main()
{
int i,n;
A temp;
cin>>n;
for(i =0; i < n; i++)
{
cin>>temp.a>>temp.b;
num.push_back(temp);
}
sort(num.begin(),num.end(),cmp);
for(i=0;i<n;i++)
{
cout<<num[i].a<<" "<<num[i].b<<endl;
}
return 0;
}
阅读全文
0 0
- sort自定义cmp函数
- c++自定义sort函数
- C++sort 函数用法
- C++sort 函数用法
- C++sort 函数用法
- C++Sort函数用法
- C++sort函数
- C/C++sort函数用法
- js中的sort函数的自定义函数
- 如何自定义sort函数中的比较函数
- C++sort函数的用法
- C++sort函数的用法
- [C++]排序函数sort()用法
- C++sort函数使用总结
- C/C++中的sort()函数,,,,,,,
- C++sort函数的用法
- C++sort函数的用法
- C++sort函数的用法
- 数据中心到底要用多少光模块?
- webstorm中vue语法没有提示,怎么解决?
- JS正则表达式(密码同时包含数字和字母)
- 4.元组tuple:戴上了枷锁的列表
- 纸牌博弈
- c++自定义sort函数
- 数据中心里的那些XDC们
- 输入n,求斐波那契数列第n项。斐波那契数列的第0项为0,第一项为1,之后的项依次为前两项之和。
- 金融企业软件测试中心筹备书
- 位运算基本概念及简单运用
- 面向5G的数据中心建、维、营
- [P2023][AHOI2009]维护序列
- jackson 完美用法
- JAVA项目连接到JDBC