sort函数中三个参数的用法
来源:互联网 发布:suse linux dns配置 编辑:程序博客网 时间:2024/05/16 10:16
1、sort函数一般只对数组元素排序,而且必须知道地址。如对a[0]到a[n-1]共n个元素进行排序,用sort(ar,ar+n)。其中n可以为1,即对一个元素也可以使用sort函数。
2、可以有如下两种形式:
1、void sort(int *first,int *last); //将数组按升序排序2、void sort(int *first,int *last,bool cmp); //cmp是一种比较的方法cmp()函数中的参数是结构体的例子如下:
//对输入的姓名排序#include<iostream>#include<string>#include<algorithm>#include<cstring>using namespace std;struct man{string name;};bool cmp(man a,man b){return strcmp(a.name.c_str(),b.name.c_str())<0;}int main(){int m;//测试用例数cin>>m;int n;//测试的人数while(m--){man ma[10];cin>>n;for(int i=0;i<n;i++){cin>>ma[i].name;}sort(ma,ma+n,cmp);for(int j=0;j<n;j++){cout<<ma[j].name;cout<<endl;}}return 0;}cmp()函数中的参数是整数的例子如下:
#include<iostream>#include<algorithm>using namespace std;bool cmp(int a,int b){return a>b;}void main(){int arr[4]={144,244,1224,2222};sort(arr,arr+4,cmp);cout<<arr[0]<<endl<<arr[1]<<endl<<arr[2]<<endl<<arr[3]<<endl;}
0 0
- sort函数中三个参数的用法
- sort函数的用法,自定义comp参数。
- C、C++和JAVA等程序语言中,函数参数三个点“...”的意思和用法。
- matlab中sort函数的用法
- matlab中sort函数的用法
- STL中sort函数的用法
- STL中sort()函数的用法详解
- matlab中sort函数的用法
- MATLAB中排序函数sort()的用法
- C++中sort()函数的用法
- matlab中sort函数的用法
- STL中sort()函数的用法
- C++中Sort函数的用法
- C++中 sort函数 的用法
- MATLAB中排序函数sort()的用法
- C++ 中sort函数的用法
- sort函数的用法
- sort函数的用法
- java设计模式14——模板方法模式(Template Method)
- OCP-1Z0-053-V13.02-453题
- 基本语法的介绍不错的网站
- 友元函数和友元类
- appStore上传苹果应用程序软件发布流程
- sort函数中三个参数的用法
- machine learning(5) --AdaBoost分类器
- 2·14 情人&元宵节专题:半质数的个数-c#求解-英雄会在线编程题目
- 调用模板类出现 undefined reference 错误的解决方法
- 内核线程的实例与摧毁
- linux中怎样从底部向上查看log文件
- ZOJ 1204 Additive equations
- TOJ 2429 Find the Clones
- Remove Nth Node From End of List