#include<algorithm>中的sort算法
来源:互联网 发布:mac用什么下载软件 编辑:程序博客网 时间:2024/06/04 22:36
1、sort函数的时间复杂度为n*log2(n),执行效率较高。
2、sort函数的形式为sort(first,end,method)//其中第三个参数可选。
3、若为两个参数,则sort的排序默认是从小到大,见如下例子
4、若为三个参数,则需要写一个cmp函数(此名称cmp可变),用于判断是从小到大排序还是从大到小排序。
(1)需要排序的数组直接为int类型,则见如下例子(从大到小排序)
#include <algorithm>#include <iostream>using namespace std;bool com(int a,int b){ return a>b;}int main(){ int a[10]={9,6,3,8,5,2,7,4,1,0}; for(int i=0;i<10;i++) cout<<a[i]<<endl; sort(a,a+10,com);//在这里就不需要对com函数传入参数 for(int i=0;i<10;i++) cout<<a[i]<<endl; return 0;}
(2)如果想依照一个结构体内的一个int型的属性参数进行排序,则见如下例子(从大到小排列)
#include <iostream>#include <algorithm>using namespace std;struct node {int a; //.........//};bool cmp(node x,node y){ if(x.a != y.a) return (x.a > y.a);}void main(void){int i;node N_t[5];for(i=0; i<5; i++){cin>>N_t[i].a;}sort(N_t, N_t+5, cmp);for(i=0; i<5; i++){cout<<N_t[i].a;}}
0 0
- #include<algorithm>中的sort算法
- <algorithm>中的sort()函数
- #include <algorithm>中sort的一般用法
- #include <algorithm>中sort的一般用法
- #include <algorithm>中sort的一般用法
- #include <algorithm>中的algorithm 什么时候需要用到
- (C#)排序算法 Sort Algorithm
- C++中的 头文件”#include<algorithm>“
- C++中的算法algorithm
- 【算法】插入排序 insert sort algorithm
- STL algorithm算法sort,stable_sort(55)
- #include<algorithm>
- #include <algorithm>
- #include<algorithm>
- #include<algorithm>
- Algorithm头文件中的sort函数使用
- sort algorithm
- sort algorithm
- python 区分版本为64位还是32位的方法
- mac下卸载mysql
- Kalman滤波相关
- 架构设计:生产者-缓存队列-消费者(1)
- ios xib label 自适应宽度
- #include<algorithm>中的sort算法
- Gradle仓库配置
- 梦断短细长流往事空
- Gradle编译框架基础
- 线程的总结
- 树莓派配置无线网卡
- 比特币协议是怎样工作的(上)
- 29.Your database instance is configured with automatic undo management and the UNDO_RETENTION
- c/c++ 数组和指针的一些不同之处