sort函数对数组排序
来源:互联网 发布:unity3d 3dmax 编辑:程序博客网 时间:2024/05/16 07:12
注意
头文件:
#include <algorithm>
using namespace std;
1.默认的sort函数是按升序排。对应于1)sort(a,a+n); //两个参数分别为待排序数组的首地址和尾地址
2.可以自己写一个cmp函数,按特定意图进行排序。对应于2)例如:int cmp( const int &a, const int &b ){ if( a > b ) return 1; else return 0;}sort(a,a+n,cmp);是对数组a降序排序又如:int cmp( const POINT &a, const POINT &b ){ if( a.x < b.x ) return 1; else if( a.x == b.x ){ if( a.y < b.y ) return 1; else return 0; } else return 0;}sort(a,a+n,cmp);
是先按x升序排序,若x值相等则按y升序排
典型代码:
//简版#include <algorithm>using namespace std; sort(a,a+20);///#include <algorithm>using namespace std;bool compare(int a,int b){ return a<b; //升序排列,如果改为return a>b,则为降序}int main(){ int a[20]={2,4,1,23,5,76,0,43,24,65},i; for(i=0;i<20;i++) printf("%d ",a[i]); printf("\n"); sort(a,a+20,compare); for(i=0;i<20;i++) printf("%d ",a[i]); printf("\n"); return 0;}
- sort函数对数组排序
- 利用sort函数对数组进行排序【java】
- 使用sort()函数对数组中的数字进行排序。
- sort函数对数组和字符串的排序
- sort函数对数组、容器以及结构体的排序(for sort)
- js 数组排序函数sort
- sort对二维字符数组排序
- sort和qsort对字符数组排序
- 使用sort方法对数组进行排序
- 利用sort对数组进行自定义排序
- 用sort对字符数组排序
- 用sort对二维数组排序
- sort对二维数组进行排序备忘
- 利用sort对结构体数组排序
- sort对二维char数组排序?
- sort对二维字符数组排序
- sort和qsort对字符数组排序
- C++ sort函数对class类排序
- 异美旅游
- 非线性曲线拟合函数 lsqcurvefit 最小二乘
- android4.2 PC端查看storage文件和MID端路径不一致问题
- 关联规则挖掘评述
- vsftpd虚拟用户配置
- sort函数对数组排序
- VB.NET 通过窗口句柄,获取webbrowser控件HTML内容
- Android connect函数的使用
- js判断IE浏览器是否安装插件
- 出奇制胜:淘宝推广手记
- 1小时ASP快速入门
- 倒腾(jdbc更改版,包括dao和测试)
- Mysql常用
- python crawler0723.py