C++sort()函数的用法
(一)为什么要用c++标准库里的排序函数
Sort()函数是c++一种排序方法之一,学会了这种方法也打消我学习c++以来使用的冒泡排序和选择排序所带来的执行效率不高的问题!因为它使用的排序方法是类似于快排的方法,时间复杂度为n*log2(n),执行效率较高!
(二)c++标准库里的排序函数的使用方法
I)Sort函数包含在头文件为#include<algorithm>的c++标准库中,调用标准库里的排序方法可以不必知道其内部是如何实现的,只要出现我们想要的结果即可!
II)Sort函数有三个参数:
(1)第一个是要排序的数组的起始地址。
(2)第二个是结束的地址(最后一位要排序的地址)
(3)第三个参数是排序的方法,可以是从大到小也可是从小到大,还可以不写第三个参数,此时默认的排序方法是从小到大排序。
Sort函数使用模板:
Sort(start,end,,排序方法)
下面就具体使用sort( )函数结合对数组里的十个数进行排序做一个说明!
例一:sort函数没有第三个参数,实现的是从小到大
#include<iostream>
#include<algorithm>
using namespacestd;
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]<<' ';
cout<<endl;
sort(a,a+10);
for(int i=0;i<10;i++)
cout<<a[i]<<' ';
cout<<endl;
return 0;
}
例二
通过上面的例子,会产生疑问:要实现从大到小的排序肿么办?
这就如前文所说需要在sort()函数里的第三个参数里做文章了,告诉程序我要从大到小排序!
需要加入一个比较函数 complare(),此函数的实现过程是这样的
bool complare(inta,int b)
{
return a>b;
}
这就是告诉程序要实现从大到小的排序的方法!
#include<iostream>
#include<algorithm>
using namespacestd;
bool complare(inta,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]<<' ';
cout<<endl;
sort(a,a+10,complare);//在这里就不需要对complare函数传入参数了,//这是规则
for(int i=0;i<10;i++)
cout<<a[i]<<' ';
cout<<endl;
return 0;
}
例三:利用sort函数还可以实现对字符的排序,排序方法大同小异,下面就把程序范例展示一下
#include<iostream>
#include<algorithm>
using namespacestd;
bool cmp2(chara,char b)
{
if(a!=b) //加上这个if语句似乎更好 ;
return a<b;
}
int main()
{
char a[11]="asdfghjklk";
for(int i=0;i<10;i++)
cout<<a[i]<<' ';
cout<<endl;
sort(a,a+10,cmp2);
for(int i=0;i<10;i++)
cout<<a[i]<<' ';
cout<<endl;
return 0;
}
例四:对结构体进行排序(对changdu排降序,zhijing排增序,编号排降序)
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespacestd;
struct info
{
int len;
int rr;
int num;
} gg[1100];
bool cmp(structinfo a,struct info b) ///排序函数/
{
if(a.len!=b.len)
return a.len>b.len;
else
{
if(a.rr!=b.rr)
return a.rr<b.rr;
else
return a.num>b.num;
}
}
int main()
{
int N,i,n;
scanf("%d",&N);
while(N--)
{
scanf("%d",&n);
for(i=0; i<n; i++)
{
scanf("%d %d%d",&gg[i].len,&gg[i].rr,&gg[i].num);
}
sort(gg,gg+n,cmp);
printf("%d\n",gg[0].num);
}
return 0;
}
例五:对含有字符串的结构体进行排序;
Sign Deliveries
Description
As a big fan of T-mall and TaoBao, shopping on 11.11 is of great importance for HoneyCat. Because she bought too many objects, how to sign deliveries was such big problem. She had the shipped date of every object(every thing would be shipped in a month), the name of the express company and the number of days it would spend on the way. Can you tell her when and where to sign those deliveries? Your answer should be sorted by the time that the delivery reaches Harbin in ascending order. If several deliveries arrived on the same day, sorted by their express companies’ name in ascending lexicographical order. If they are still same, you should sort the objects’ name in the same way.
input
At the first line, there is an integer T (T ≤ 233), indicates the number of test cases.
In each case, the first line is an integer n, the total number of objects (0 < n ≤ 233).
Then in the following n lines, every line contains two integers m, p and two words, s1, s2. m is the date the company shipped the object, p is the number of days it needed to reach Harbin, s1 is the name of the express company, and s2 is the name of the object (0<m, p<31, the length of s1 and s2 are both in [1, 233]).There will only be ’a’-’z’, ’A’-’Z’ in the words, case sensitive.
output
every data one one line ,the form is : s1 space s2;
Sample
Input | Output |
1 3 15 8 ZhongTong StampaTshirt 20 3 YuanTong IceSwordSkates 1 1 DangDang LittlePrince | YuanTong IceSwordSkates ZhongTong StampaTshirt Dangdang LittlePrince
|
hint
If the date is smaller than or equal to 11, the object would be shipped in December.
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
/*
int main()
{
char s1[100],s2[100];
while(~scanf("%s%s",s1,s2)!=EOF)
{
if(strcmp(s1,s2)>0)
printf("%s",s2);
else
printf("%s",s1);
printf("\n");
}
return 0;
}
*/
struct tosort
{
int days;
char s1[250];
char s2[250];
}ob[250];
int cmp(tosort x,tosort y) //排序函数;
{
if(x.days!=y.days)
return x.days<y.days;
else if(strcmp(x.s1,y.s1)!=0) //对字符串s1进行排序操作;
return strcmp(y.s1,x.s1)>0;
else if(strcmp(x.s2,y.s2)!=0) //对字符串s2进行排序操作;
return strcmp(y.s2,x.s2)>0;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n,a,b;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d%d",&a,&b);
if(a>11)
ob[i].days=a+b-11;
else if(a<=11)
ob[i].days=30+a+b-11;
scanf("%s%s",ob[i].s1,ob[i].s2);
}
sort(ob,ob+n,cmp);
for(int i=0;i<n;i++)
printf("%s %s\n",ob[i].s1,ob[i].s2);
}
return 0;
}
例六:对 vector 进行排序;
1.
#include<cstdio>
#include<vector>
int main()
{
vector<int >vec;
......
sort(vec.begin(),vec.end());
}
#include<cstdio>
#include<vector>
int c[maxn];
int cmp(const int &a,const int &b)
{
return c[a]<c[b];
}
int main()
{
vector<int >vec[maxn];
......
for(int i=1;i<maxn;i++)
sort(vec[i].begin(),vec[i].end(),cmp);
//这个排序的意思是,vec里保存的是下标,然后按照下标所对应的费用来对下标进行排序;
}