Sort函数详细讲解
顾名思义,sort就是用来排序的函数,它根据具体的情形使用不同的排序办法,效率较高。
1.如何使用sort排序
sort函数的使用必须加上头文件“#include
具体使用方式如下:
sort(a,b,c);
其中a为:首元素地址,必填内容
b为:尾元素地址的下一个地址,必填内容
c为:比较函数(也即比较规则),非必填内容
a,b为必填内容,c根据实际情况填写,非必填,如果不填比较函数,则默认对给出的区间进行升序排序(字符串为字典序)
1.1对整型数组排序:
#include <iostream>
#include <algorithm>
using namespace std;int main(){int a[6] = {9, 4, 2, 5, 6, -1};cout << "排序前:";for(int i = 0;i < 6;i ++) cout << a[i] << " ";cout << endl;sort(a + 0, a+4); //对a[0]~a[3]升序排序(从小到大)cout << "第一次排序:";for(int i = 0;i < 6;i ++) cout << a[i] << " ";cout << endl;sort(a, a+6); //对a[0]~a[5]升序排序(从小到大)cout << "第二次排序:";for(int i = 0;i < 6;i ++) cout << a[i] << " ";return 0;
}
运行结果如下:
排序前:9 4 2 5 6 -1
第一次排序:2 4 5 9 6 -1
第二次排序:-1 2 4 5 6 9
1.2对浮点型数组排序:
#include <iostream>
#include <algorithm>
using namespace std;int main(){double a[] = {1.1, -1,1, 2.5, -3.3, 5.5, 1.6};cout << "排序前:";for(int i = 0;i < 6;i ++) cout << a[i] << " ";cout << endl;sort(a, a+6); //对a[0]~a[5]升序排序(从小到大)cout << "排序后:";for(int i = 0;i < 6;i ++) cout << a[i] << " ";cout << endl;return 0;
}
运行结果如下:
排序前:1.1 -1 1 2.5 -3.3 5.5
排序后:-3.3 -1 1 1.1 2.5 5.5
1.3对字符串(字符数组)排序:
#include <iostream>
#include <algorithm>
using namespace std;int main(){char a[] = {'I', 'L', 'O', 'V', 'E', 'U'};cout << "排序前:";for(int i = 0;i < 6;i ++) cout << a[i];cout << endl;sort(a, a+6); //对a[0]~a[5]按照字典序排序cout << "排序后:";for(int i = 0;i < 6;i ++) cout << a[i];cout << endl;return 0;
}
运行结果如下:
排序前:ILOVEU
排序后:EILOUV
2.如何实现比较函数cmp
下面介绍对于基本数据类型,结构体类型,进行自定义排序cmp的写法
具体方式如下:
2.1基本数据类型数组的排序:
若比较函数不填,默认升序(或者字典序)排序,下面对int数组的排序;
#include <iostream>
#include <algorithm>
using namespace std;int main(){int a[] = {3, 5, 2, 1, 4};cout << "排序前:";for(int i = 0;i < 5;i ++) cout << a[i] << " ";cout << endl;sort(a, a+5);cout << "排序后:";for(int i = 0;i < 5;i ++) cout << a[i] << " ";return 0;
}
运行结果如下:
排序前:3 5 2 1 4
排序后:1 2 3 4 5
如果想要降序排序,那么我们就要用到cmp函数“告诉”sort何时交换元素,代码写法如下:
#include <iostream>
#include <algorithm>
using namespace std;bool cmp(int a, int b){return a > b; //可以理解为a > b时 就把a放前面
}int main(){int a[] = {3, 5, 2, 1, 4};cout << "排序前:";for(int i = 0;i < 5;i ++) cout << a[i] << " ";cout << endl;sort(a, a+5, cmp);cout << "排序后:";for(int i = 0;i < 5;i ++) cout << a[i] << " ";return 0;
}
运行结果如下:
排序前:3 5 2 1 4
排序后:5 4 3 2 1
这样就可以让数值大的元素在前面,即降序排序,对于double,char亦如此,把cmp内传入的参数类型改一下就好了
2.2结构体数组的排序
现在定义了如下的结构体:
struct node{int x, y;
}ssd[10];
如果想要ssd数组按照x从大到小排序,可以这样写cmp函数:
bool cmp(node a, node b){return a.x > b.x;
}
完整示例:
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;struct node{int x, y;
}ssd[10];bool cmp(node a, node b){return a.x > b.x; //按照x降序排列
}int main(){ssd[0].x=2; //{2,2} ssd[0].y=2;ssd[1].x=1; //{1,3}ssd[1].y=3;ssd[2].x=3; //{3,1}ssd[2].y=1;sort(ssd, ssd+3, cmp);for(int i=0;i<3;i++){printf("%d %d\n",ssd[i].x,ssd[i].y);}return 0;
}
运行结果如下:
3 1
2 2
1 3
如果想要先按照x降序,x相等时,y升序排列(二级排序),那么代码实现如下:
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;struct node{int x, y;
}ssd[10];bool cmp(node a, node b){if(a.x!=b.x) return a.x>b.x;//按照x降序排列 else return a.y < b.y; //按照y升序排列
}int main(){ssd[0].x=2; //{2,2} ssd[0].y=2;ssd[1].x=1; //{1,3}ssd[1].y=3;ssd[2].x=2; //{2,1}ssd[2].y=1;sort(ssd,ssd+3,cmp);for(int i=0;i<3;i++){printf("%d %d\n",ssd[i].x,ssd[i].y);}return 0;
}
运行结果如下:
2 1
2 2
1 3
C++全排列函数详解
C++STL中有两种关于全排列的函数:next_premutation() 与 prev_premutation()
接下来我们分别对两个函数进行讲解
1. next_premutation()函数
原理:
next_permutation()函数用于生成当前序列的下一个排列,它按照字典序对序列进行重新排列,如果存在下一个排列,则将当前序列更改为下一个排列,并返回true;如果当前序列已经是最后一个排列,则将序列更改为第一个排列,并返回false。
代码示例:
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{ int num[3]={1,2,3}; do { //输出当前数组 cout<<num[0]<<" "<<num[1]<<" "<<num[2]<<endl; //如果存在下一个排列就进入循环//存在就表明(next_permutation()的值为真,反之为假) }while(next_permutation(num,num+3)); //输出循环结束后的数组,验证“下一个排列不存在时数组是否被重新排列了,也即回到第一个排列” cout<<num[0]<<" "<<num[1]<<" "<<num[2]<<endl;return 0;
}
运行结果:
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
1 2 3
2. prev_premutation()函数
原理:
prev_permutation()函数用于生成当前序列的上一个排列,它按照字典序对序列进行重新排列,如果存在上一个排列,则将当前序列更改为上一个排列,并返回true;如果当前序列已经是第一个排列,则将序列更改为最后一个排列,并返回false。
代码示例:
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{ int num[3]={3,2,1}; do { //输出当前数组 cout<<num[0]<<" "<<num[1]<<" "<<num[2]<<endl; //如果存在上一个排列就进入循环//存在就表明(prev_permutation()的值为真,反之为假) }while(prev_permutation(num,num+3)); //输出循环结束后的数组,验证“上一个排列不存在时数组是否被重新排列了也即回到最后一个排列” cout<<num[0]<<" "<<num[1]<<" "<<num[2]<<endl;return 0;
}
运行结果:
3 2 1
3 1 2
2 3 1
2 1 3
1 3 2
1 2 3
3 2 1