C++中sort函数使用方法
2024-08-30 02:06:08
一.sort函数
1.sort函数包含在头文件为#include<algorithm>的c++标准库中,调用标准库里的排序方法可以实现对数据的排序,但是sort函数是如何实现的,我们不用考虑!
2.sort函数的模板有三个参数:
void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);
(1)第一个参数first:是要排序的数组的起始地址。
(2)第二个参数last:是结束的地址(最后一个数据的后一个数据的地址)
(3)第三个参数comp是排序的方法:可以是从升序也可是降序。如果第三个参数不写,则默认的排序方法是从小到大排序。
3.实例
#include<iostream>
#include<algorithm>
using namespace std;
main()
{
//sort函数第三个参数采用默认从小到大
int a[]={,,,,,,,,,};
sort(a,a+);
for(int i=;i<;i++)
cout<<a[i]<<" ";
}
运行结果:
#include<iostream>
#include<algorithm>
using namespace std;
bool cmp(int a,int b);
main(){
//sort函数第三个参数自己定义,实现从大到小
int a[]={,,,,,,,,,};
sort(a,a+,cmp);
for(int i=;i<;i++)
cout<<a[i]<<" ";
}
//自定义函数
bool cmp(int a,int b){
return a>b;
}
运行结果:
#include<iostream>
#include<algorithm>
#include"cstring"
using namespace std;
typedef struct student{
char name[];
int math;
int english;
}Student;
bool cmp(Student a,Student b);
main(){
//先按math从小到大排序,math相等,按english从大到小排序
Student a[]={{"apple",,},{"limei",,},{"apple",,}};
sort(a,a+,cmp);
for(int i=;i<;i++)
cout<<a[i].name <<" "<<a[i].math <<" "<<a[i].english <<endl;
}
bool cmp(Student a,Student b){
if(a.math >b.math )
return a.math <b.math ;//按math从小到大排序
else if(a.math ==b.math )
return a.english>b.english ; //math相等,按endlish从大到小排序
}
运行结果
4.对于容器,容器中的数据类型可以多样化
1) 元素自身包含了比较关系,如int,double等基础类型,可以直接进行比较greater<int>() 递减, less<int>() 递增(省略)
#include<iostream>
#include<algorithm>
#include"vector"
using namespace std;
typedef struct student{
char name[];
int math;
int english;
}Student;
bool cmp(Student a,Student b);
main(){
int s[]={,,,,};
vector<int>arr(s,s+);
sort(arr.begin(),arr.end(),greater<int>());
for(int i=;i<arr.size();i++)
cout<<arr[i]<<" ";
}
运行结果:
2)元素本身为class或者struct,类内部需要重载< 运算符,实现元素的比较;
注意事项:bool operator<(const className & rhs) const; 如何参数为引用,需要加const,这样临时变量可以赋值;重载operator<为常成员函数,可以被常变量调用;
#include<iostream>
#include<algorithm>
#include"vector"
using namespace std;
typedef struct student{
char name[];
int math;
//按math从大到小排序
inline bool operator < (const student &x) const {
return math>x.math ;
}
}Student;
main(){
Student a[]={{"apple",},{"limei",},{"apple",}};
sort(a,a+);
for(int i=;i<;i++)
cout<<a[i].name <<" "<<a[i].math <<" " <<endl;
}
运行结果:
重载<也可以定义为如下格式:
struct Cmp{
bool operator()(Info a1,Info a2) const {
return a1.val > a2.val;
}
};
最新文章
- PowerDesigner(数据建模)使用大全
- SRETAN
- js选项卡
- 推荐一款炫酷的提示框插件SweetAlert
- JS高级程序设计2nd部分知识要点5
- TLV格式是什么格式
- 在Oracle中更新数据时,抛出:ORA-01008: not all variables bound
- Minimum Window Substring &;&;&; Longest Substring Without Repeating Characters 快慢指针,都不会退,用hashmap或者其他结构保证
- Android带参数链接请求服务器
- Java学习日记-5 关键字static和final 以及接口
- 简单OC程序
- 转: seajs手册与文档之--模块定义
- Hi,腾讯WeTest联合Unity官方打造的性能分析工具UPA,今日全新发布!
- hdu2089 不要62--经典数位DP
- Ubuntu 无法进行SSH连接,开启22端口
- Kubernetes与容器设计模式
- MacBook快速入门
- bootStrap中的ul导航2
- 设计模式之生成者模式java源代码
- Javascript富文本编辑器
热门文章
- Spring MVC 设置UTF-8编码
- LVS+Heartbeat安装部署文档
- Phaserjs怎样用ES6开发游戏
- windows脚本复制文件(将u盘文件复制到固定路径)
- c语言中";->;";和";.";的区别
- github上创建项目
- POJ 3692 幼儿园做游戏 最大团 模板题
- python web Tornado框架
- java.lang.Integer.MAX_VALUE;这是什么意思?
- redis-sentinel 主从复制高可用