C++ sort()排序函数用法详解(深入了解,一文学会)

        C++ STL 标准库提供有很多实用的排序函数,如表 1 所示。通过调用它们,我们可以很轻松地实现对普通数组或者容器中指定范围内的元素进行排序。

   本文作者原创,转载请附上文章出处与本文链接。

C++ sort()排序函数用法详解(深入了解,一文学会)目录

1 sort()函数

2 stable_sort()函数

3 nth_element()函数


1 sort()函数

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

sort() 函数是基于快速排序实现的。

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

  1. 容器支持的迭代器类型必须为随机访问迭代器。这意味着,sort() 只对 array、vector、deque 这 3 个容器提供支持。
  2. 如果对容器中指定区域的元素做默认升序排序,则元素类型必须支持<小于运算符;同样,如果选用标准库提供的其它排序规则,元素类型也必须支持该规则底层实现所用的比较运算符;
  3. sort() 函数在实现排序时,需要交换容器中元素的存储位置。这种情况下,如果容器中存储的是自定义的类对象,则该类的内部必须提供移动构造函数和移动赋值运算符。
#include <iostream>     // std::cout
#include <algorithm>    // std::sort
#include <vector>       // std::vector
//以普通函数的方式实现自定义排序规则
bool mycomp(int i, int j) {
    return (i < j);
}
//以函数对象的方式实现自定义排序规则
class mycomp2 {
public:
    bool operator() (int i, int j) {
        //从小到大
        return (i < j);
        //return (i > j) 从大到小
    }
};
int main() {
    std::vector<int> myvector{ 32, 71, 12, 45, 26, 80, 53, 33 };
    //调用第一种语法格式,对 32、71、12、45 进行排序
    std::sort(myvector.begin(), myvector.begin() + 4); //(12 32 45 71) 26 80 53 33
    //调用第二种语法格式,利用STL标准库提供的其它比较规则(比如 greater<T>)进行排序
    std::sort(myvector.begin(), myvector.begin() + 4, std::greater<int>()); //(71 45 32 12) 26 80 53 33

    //调用第二种语法格式,通过自定义比较规则进行排序
    std::sort(myvector.begin(), myvector.end(), mycomp2());//12 26 32 33 45 53 71 80
    //输出 myvector 容器中的元素
    for (std::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it) {
        std::cout << *it << ' ';
    }
    return 0;
}

2 stable_sort()函数

有些场景是需要保证相等元素的相对位置的。例如对于一个保存某种事务(比如银行账户)的容器,在处理这些事务之前,为了能够有序更新这些账户,需要按照账号对它们进行排序。而这时就很有可能出现相等的账号(即同一账号在某段时间做多次的存取钱操作),它们的相对顺序意味着添加到容器的时间顺序,此顺序不能修改,否则很可能出现账户透支的情况。

值得一提的是,stable_sort() 函数完全可以看作是 sort() 函数在功能方面的升级版。换句话说,stable_sort() 和 sort() 具有相同的使用场景,就连语法格式也是相同的(后续会讲),只不过前者在功能上除了可以实现排序,还可以保证不改变相等元素的相对位置。

#include <iostream>     // std::cout
#include <algorithm>    // std::sort
#include <vector>       // std::vector

//以普通函数的方式实现自定义排序规则
bool mycomp(int i, int j) {
	return (i < j);
}
//以函数对象的方式实现自定义排序规则
class mycomp2 {
public:
	bool operator() (int i, int j) {
		return (i < j);
	}
};
int main()
{
	std::vector<int> myvector{ 32, 71, 12, 45, 26, 80, 53, 33 };
	//调用第一种语法格式,对 32、71、12、45 进行排序
	std::stable_sort(myvector.begin(), myvector.begin() + 4); //(12 32 45 71) 26 80 53 33
	//调用第二种语法格式,利用STL标准库提供的其它比较规则(比如 greater<T>)进行排序
	std::stable_sort(myvector.begin(), myvector.begin() + 4, std::greater<int>()); //(71 45 32 12) 26 80 53 33
	//调用第二种语法格式,通过自定义比较规则进行排序,这里也可以换成 mycomp2()
	std::stable_sort(myvector.begin(), myvector.end(), mycomp);//12 26 32 33 45 53 71 80
	//输出 myvector 容器中的元素
	for (std::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it) {
		std::cout << *it << ' ';
	}

	system("pause");
}

3 nth_element()函数

只有普通数组和符合以下全部条件的容器,才能使用使用 nth_element() 函数:

  1. 容器支持的迭代器类型必须为随机访问迭代器。这意味着,nth_element() 函数只适用于 array、vector、deque 这 3 个容器。
  2. 当选用默认的升序排序规则时,容器中存储的元素类型必须支持 <小于运算符;同样,如果选用标准库提供的其它排序规则,元素类型也必须支持该规则底层实现所用的比较运算符;
  3. nth_element() 函数在实现过程中,需要交换某些元素的存储位置。因此,如果容器中存储的是自定义的类对象,则该类的内部必须提供移动构造函数和移动赋值运算符。
#include <iostream>     // std::cout
#include <algorithm>    // std::sort
#include <vector>       // std::vector

//以普通函数的方式自定义排序规则
bool mycomp1(int i, int j) {
	return (i > j);
}
//以函数对象的方式自定义排序规则
class mycomp2 {
public:
	bool operator() (int i, int j) {
		return (i > j);
	}
};
using namespace std;

int main()
{
	std::vector<int> myvector{ 3,1,2,5,4 };
	//默认的升序排序作为排序规则
	std::nth_element(myvector.begin(), myvector.begin() + 2, myvector.end());
	cout << "第一次nth_element排序:\n";
	for (std::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it) {
		std::cout << *it << ' ';
	}
	//自定义的 mycomp2() 或者 mycomp1 降序排序作为排序规则
	std::nth_element(myvector.begin(), myvector.begin() + 3, myvector.end(), mycomp1);
	cout << "\n第二次nth_element排序:\n";
	for (std::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it) {
		std::cout << *it << ' ';
	}

	system("pause");
}

 

  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

双子座断点

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值