声明简介、比较函数、结构体排序
开始
- 排序用的函数是sort函数,包含在< algorithm >文件中。
- sort()不仅可以用来给数字排序,还可以给字符串排序,默认的是升序,如果需要降序的话,可以自己写一个cmp回调函数(函数名可自拟,通常用cmp),然后在sort函数中,加上(cmp)函数名即可。
- 实现原理:sort并不是简单的快速排序,它对普通的快速排序进行了优化,此外,它还结合了插入排序和推排序。系统会根据你的数据形式和数据量自动选择合适的排序方法,这并不是说它每次排序只选择一种方法,它是在一次完整排序中不同的情况选用不同方法,比如给一个数据量较大的数组排序,开始采用快速排序,分段递归,分段之后每一段的数据量达到一个较小值后它就不继续往下递归,而是选择插入排序,如果递归的太深,他会选择推排序。
具体函数实现,请参考:http://www.cnblogs.com/fengcc/p/5256337.html
声明简介
函数声明
| 12
 3
 4
 5
 6
 7
 8
 
 | #include <algorithm>
 template< class RandomIt(随机迭代器) >
 void sort( RandomIt first, RandomIt last );
 
 template< class RandomIt, class Compare >
 void sort( RandomIt first, RandomIt last, Compare comp );
 
 
 | 
形式:sort(first_pointer,first_pointer+n,cmp);
参数解释: 第一个参数是数组的首地址,一般写上数组名就可以,因为数组名是一个指针常量。第二个参数相对较好理解,即首地址加上数组的长度n(代表尾地址的下一地址)。最后一个参数是比较函数的名称(自定义函数cmp),这个比较函数可以不写,即第三个参数可以缺省,这样sort会默认按数组升序排序。
简单例子:对数组A的0~n-1元素进行升序排序,只要写sort(A,A+n)即可;对于向量V也一样,sort(v.begin(),v.end())即可。
sort扩展-比较函数
自己定义比较函数
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 
 | int A[100];
 bool cmp1(int a, int b)
 {
 return a > b;
 
 }
 sort(A, A+100, cmp1);
 
 
 Student Stu[100];
 bool cmp2(Student a, Student b)
 {
 return a.id>b.id;
 
 }
 sort(Stu,Stu+100,cmp2);
 
 | 
使用标准库函数
functional提供了一堆基于模板的比较函数对象,它们是:equal_to、not_equal_to、greater、greater_equal、less、less_equal。这些东西的用法看名字就知道了。在这里,我么sort要用到的也只是greater和less就足够了,用法如下:
- 升序:sort(begin,end,less())
- 降序:sort(begin,end,greater())
缺点:也只是实现简单的排序,结构体不适用。
| 12
 3
 4
 5
 6
 7
 8
 9
 
 | #include <iostream>#include <cstdio>
 #include <algorithm>
 #include <functional>
 
 using namespace std;
 
 sort(A,A+100,greater<int>());
 sort(A,A+100,less<int>());
 
 | 
重载结构体或类的比较运算符
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 
 | typedef struct Student{
 int id;
 string name;
 double grade;
 
 bool operator<(const Student& s)
 {
 return id>s.id;
 
 }
 };
 vector<Student> V;
 sort(V.begin(),V.end());
 
 
 vector<Student> V;
 bool operator<(const Student& s1, const Student& s2)
 {
 return s1.id>s2.id;
 
 }
 sort(V.begin(),V.end());
 
 | 
注意:一定要重载<运算符,因为系统默认是降序,用的是<运算符。
声明比较类(少用)
| 12
 3
 4
 5
 6
 7
 8
 
 | struct Less{
 bool operator()(const Student& s1, const Student& s2)
 {
 return s1.id<s2.id;
 }
 };
 sort(sutVector.begin(),stuVector.end(),Less());
 
 | 
(参考自:sort函数详解)
结构体排序
- 有三个人(Person结构体),每个人都有name(string型)和age(int型)两个属性,现在需要按照下面的规则排序:先以姓名按从小到大排序(如abc<abd),如果姓名相同,则按照年龄从大到小排序。
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 
 | #include<iostream>#include<algorithm>
 #include<string>
 using namespace std;
 struct Person{
 string name;
 int age;
 };
 bool cmp(Person a,Person b){
 if(a.name!=b.name){
 return a.name<b.name;
 } else {
 return a.age>b.age;
 }
 }
 int main(){
 Person p[3];
 int i,j;
 for(i=0;i<3;i++){
 cin>>p[i].name>>p[i].age;
 }
 
 sort(p,p+3,cmp);
 
 for(i=0;i<3;i++){
 cout<<p[i].name<<' '<<p[i].age<<endl;
 }
 }
 
 | 
- 对N个人(Person)进行排序,规则如下:每个人有3个属性,name(string型),age(int型),height(int型)。优先按照name由大到小排名,name相同的则按照age由大到小排序,age相同的则按照height由大到小排序。
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 
 | #include <iostream>#include <algorithm>
 #include <string>
 using namespace std;
 struct Person{
 string name;
 int age;
 int height;
 };
 
 bool cmp(Person a, Person b)
 {
 if(a.name != b.name)
 return a.name > b.name;
 if(a.name == b.name && a.age != b.age)
 return a.age > b.age;
 if(a.name == b.name && a.age == b.age)
 return a.height > b.height;
 }
 
 int main()
 {
 int n;
 cin >> n;
 Person p[n];
 int i, j;
 for(i=0; i<n; i++)
 cin>> p[i].name >> p[i].age >> p[i].height;
 sort(p, p + n, cmp);
 for(i=0; i<n; i++)
 {
 cout << p[i].name <<' '
 << p[i].age <<' '
 << p[i].height <<endl;
 }
 return 0;
 }
 
 | 
参考自:
[结构体或对象排序]