sort()函数用法

C++ STL 标准库中的 sort()  函数,本质就是一个模板函数。正如表 1 中描述的,该函数专门用来对容器或普通数组中指定范围内的元素进行排序,排序规则默认以元素值的大小做升序排序,除此之外我们也可以选择标准库提供的其它排序规则(比如std::greater<T>降序排序规则),甚至还可以自定义排序规则。

 

需要注意的是,sort() 函数受到底层实现方式的限制,它仅适用于普通数组和部分类型的容器。换句话说,只有普通数组和具备以下条件的容器,才能使用 sort() 函数:

  1. 容器支持的迭代器类型必须为随机访问迭代器。这意味着,sort() 只对 array、vector、deque 这 3 个容器提供支持。
  2. 如果对容器中指定区域的元素做默认升序排序,则元素类型必须支持<小于运算符;同样,如果选用标准库提供的其它排序规则,元素类型也必须支持该规则底层实现所用的比较运算符;
  3. sort() 函数在实现排序时,需要交换容器中元素的存储位置。这种情况下,如果容器中存储的是自定义的类对象,则该类的内部必须提供移动构造函数和移动赋值运算符。


另外还需要注意的一点是,对于指定区域内值相等的元素,sort() 函数无法保证它们的相对位置不发生改变。例如,有如下一组数据:

2 1 2 3 2

可以看到,该组数据中包含多个值为 2 的元素,此时如果使用 sort() 函数进行排序,则值为 2 的这 3 个元素的相对位置可能会发生改变,比如排序结果为:

1 2 2 2 3

可以看到,原本红色的元素 2 位于绿色 2 和橙色 2 的左侧,但经过 sort() 函数排序之后,它们的相对位置发生了改变,即红色 2 移动到了绿色 2 和橙色 2 的右侧。

(实际场景中,如果需要保证值相等元素的相对位置不发生改变,可以选用 stable_sort() 排序函数

1、头文件

sort() 函数位于<algorithm>头文件中,因此在使用该函数前,程序中应包含如下语句:

#include <algorithm>

2.函数调用

sort() 函数有 2 种用法,其语法格式分别为:

//对 [first, last) 区域内的元素做默认的升序排序
void sort (RandomAccessIterator first, RandomAccessIterator last);
//按照指定的 comp 排序规则,对 [first, last) 区域内的元素进行排序
void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);

其中,first 和 last 都为随机访问迭代器,它们的组合 [first, last) 用来指定要排序的目标区域;另外在第 2 种格式中,comp 可以是 C++ STL 标准库提供的排序规则(比如 std::greater<T>),也可以是自定义的排序规则。

 

关于如何自定义一个排序规则,除了《C++ STL关联式容器自定义排序规则》一节介绍的 2 种方式外,还可以直接定义一个具有 2 个参数并返回 bool 类型值的函数作为排序规则。

3.例题

#include<iostream>
#include<algorithm>
using namespace std;
main()
{
  //sort函数第三个参数采用默认从小到大
  int a[]={45,12,34,77,90,11,2,4,5,55};
  sort(a,a+10);
  for(int i=0;i<10;i++)
  cout<<a[i]<<" ";
}
#include<iostream>
#include<algorithm>
using namespace std;
bool cmp(int a,int b);
main(){
  //sort函数第三个参数自己定义,实现从大到小
  int a[]={45,12,34,77,90,11,2,4,5,55};
  sort(a,a+10,cmp);
  for(int i=0;i<10;i++)
    cout<<a[i]<<" ";
}
//自定义函数
bool cmp(int a,int b){
  return a>b;
}

 

 #include<iostream>
 #include<algorithm>
 #include"cstring"
 using namespace std;
 typedef struct student{
   char name[20];
   int math;
   int english;
 }Student;
 bool cmp(Student a,Student b);
 main(){
   //先按math从小到大排序,math相等,按english从大到小排序
   Student a[4]={{"apple",67,89},{"limei",90,56},{"apple",90,99}};
   sort(a,a+3,cmp);
   for(int i=0;i<3;i++)
   cout<<a[i].name <<" "<<a[i].math <<" "<<a[i].english <<endl;
 }
 bool cmp(Student a,Student b){
   if(a.math >b.math )
   return a.math <b.math ;//按math从小到大排序
   else if(a.math ==b.math )
       return a.english>b.english ; //math相等,按endlish从大到小排序23
 }

 

4.对于容器,容器中的数据类型可以多样化

  1) 元素自身包含了比较关系,如int,double等基础类型,可以直接进行比较greater<int>() 递减, less<int>() 递增(省略)

关键代码:

sort(arr.begin(),arr.end(),greater<int>());

2)元素本身为class或者struct,类内部需要重载< 运算符,实现元素的比较;

 注意事项:bool operator<(const className & rhs) const;  如何参数为引用,需要加const,这样临时变量可以赋值;重载operator<为常成员函数,可以被常变量调用; 

 

复制代码
 1 #include<iostream>
 2 #include<algorithm>
 3 #include"vector"
 4 using namespace std;
 5 typedef struct student{
 6     char  name[20];
 7     int math;
 8     //按math从大到小排序 
 9     inline bool operator < (const student &x) const {
10         return math>x.math ;
11     }
12 }Student;
13 main(){
14     Student a[4]={{"apple",67},{"limei",90},{"apple",90}};
15     sort(a,a+3);
16     for(int i=0;i<3;i++)    
17         cout<<a[i].name <<" "<<a[i].math <<" " <<endl;     
18 }
复制代码

运行结果:

重载<也可以定义为如下格式:

 

1 struct Cmp{
2     bool operator()(Info a1,Info a2) const {
3     return a1.val > a2.val;
4     }
5 };

 转载于:C++中sort函数使用方法 - 俊宝贝 - 博客园 (cnblogs.com)

              C++ sort()排序函数用法详解 (biancheng.net)

 

posted @ 2022-02-16 16:59  Grit_L。  阅读(802)  评论(0编辑  收藏  举报