STL排序函数

本文详细介绍了标准模板库(STL)中的排序算法,包括sort、stable_sort、partial_sort等函数的功能及使用方法,并对比了它们之间的区别。同时,还介绍了partition、stable_partition、merge和inplace_merge等函数的应用场景。

标准模板库(STL)在库头文件<algorithm>中提供了几个排序函数。STL类List就有自己的内置的sort函数。对容器来说,List的sort函数比一般的排序算法快,因为它对列表进行了优化,而且是交换指针,不是复制对象。在排序双向队列、字符串、数组、或矢量时,使用一般的sort函数,注意这个sort函数不稳定,值相同的元素不能保证处于相同的顺序位置上,为此应使用stable_sort函数。只有在需要对一个区域的元素进行排序或分区时,其他排序算法才比较高效,如果只需要对一定区域的元素进行排序,可以使用partial_sort。该区域在参数中指定,partial_sort_copy函数会生成一个容器,其中包含指定区域的元素。Nth_element 函数可以对一定区域的元素进行部分排序,但只能保证第n个元素是分界点。在数组中第n个元素左边的所有元素会小于或等于第n 个,第n个元素右边的所有元素则大于第n个元素的值。注意在任意顺序中都不需要指定区域。Nth_element函数用于确定中间点或百分点。
在根据某个条件对元素进行排序时,不是像nth_element函数那样对前n个元素排序,而是使用partion或stable_parition函数代替nth_element函数。这两个函数把容器分成两个区域,第一个区域包含满足特定条件的元素,第二个区域包含不满足特定条件的元素。最后,<algorithm>还包含merge和inplace_merge函数。但是,这两个函数都把已排序的区域作为参数,所以他们仅执行归并排序的一次调用。函数partition或stable_partition要求使用双向迭代器,而其他STL排序函数要求使用随机访问的迭代器。函数的规范如下所示。注意每个函数都有附加参数来确定排序的顺序,默认的比较运算符<。

void sort(RandomIter first,RandomIter last); void sort(RandomIter first,RandomIter last,Compare cmp); //Sorts first to last into ascending order by default //A comparison function object may be supplied void stable_sort(RandomIter first,RandomIter last); void stable_sort(RandomIter first,RandomIter last ,Compare cmp); //Sorts first to last into ascending order by fefault //A comparison function object may be supplied //Perserves original ordering of equivalent elements void partial_sort(RandomIter first,RandomIter middle,RandomIter last); void partial_sort(RandomIter first,RandomIter middle,RandomIter last,Compare cmp); //Sorts the number of elements from first to last //and places them in the range from first to middle //Elements from middle to last are not ordered //Default sort is in ascending order //A comparison function object may be supplied RandomIter partial_sort_copy(InputIter first,Input last,RandomIter first2, RandomIter last2); RandomIter partial_sort_copy(InputIter first,Input last,RandomIter first2, RandomIter last2,Compare cmp); //Sorts the number of elements from first to last //and copies them into a comtainer in the rang from //first2 to last2. //default sort is in asending order //A comparison function object may be supplied void nth_element(RandomIter first,RandomIter nth,RandomIter last); void nth_element(RandomIter first,RandomIter nth,RandomIter last,Compare cmp); //the nth element becomes a dividing point for the container //The range from first to nth ans nth to last are not sorted //all elements from the first to nth are less than or equal to nth; //all elements from the nth to last are greater than nth; //A comparison function object may be supplied BiIter partition(BiIter first,BiIter last,Predicate p); //the container is partitioned to place all elements that //satisfy a particular predicate predicate p before every element that //does not satisfy the predicate p. //the two ranges are not sorted //the return iterator points to either the first element that //does not satisfy the predicate p or the end; //Relative order of equivalent elements is not maintained BiIter stable_partition(BiIter first,BiIter last,Predicate p); //the container is partitioned to place all elements that //satisfy a particular predicate p before every element that //the two ranges are not sorted //the return iterator points to either the first element that //dosnot satisfy the predicate p or the end //relative order of equivalent elements is maintained OutputIter merge(InputIter first ,InputIter last,InputIter first2, InputIter last2,OutputIter res); OutputIter merge(InputIter first,InputIter last,InputIter first2, InputIter last2,OutputIter res,Compare cmp); //Takes two sorted ranges and merges them into anoter sorted container //A comparison function object may be supplied //For equivalent elements ,elements from the first range //will orecede elements from the second void inplace_merge(BiIter first,BiIter middle,BiIter last); void inplace_merge(BiIter first ,BiIter middle ,BiIter last,Compare cmp); //Takes two sorted ranges and merges them in place //the two ranges are first to middle and middle to last //A comparison function object may be supplied //For equivalent elements,elements from the first range //will orecede elements from the second.


### C++ STL 排序函数的使用方法 #### 使用 `std::sort` 对基本数据类型容器进行排序 对于内置类型的容器,可以直接利用标准库中的 `std::sort` 函数来完成升序排列操作[^2]。 ```cpp #include <algorithm> #include <iostream> #include <vector> int main() { std::vector<int> v{5, 3, 7, 4, 8}; // 调用 std::sort 进行默认从小到大排序 std::sort(v.begin(), v.end()); for(auto& elem : v){ std::cout << elem << ' '; } } ``` 此段代码展示了如何通过传递迭代器范围给 `std::sort` 来实现对整个向量元素按数值大小顺序重新安排位置。 #### 定制比较谓词用于自定义对象排序 当处理非原始类型的数据结构时,则需提供额外的信息告知编译器应该如何判断两个实例之间的相对次序。这通常涉及到重载小于运算符 `<` 或者指定一个二元谓词作为第三个参数传入 `std::sort` 中。 ```cpp struct Person { int age; }; // 假设已经实现了 operator< 的版本 bool operator<(const Person &lhs, const Person &rhs) { return lhs.age < rhs.age; } void customSortExample(){ std::vector<Person> people{{20}, {18}, {25}}; // 利用了全局作用域下的 operator< std::sort(people.begin(), people.end()); } ``` 上述例子说明了如果类内部定义好了合适的比较逻辑,那么就可以像对待基础类型那样简单地应用 `std::sort` 方法来进行排序工作。 另外一种方式是在调用 `std::sort` 时不依赖于成员或自由函数形式的操作符重载,而是直接给出匿名 lambda 表达式或其他形式的一元/二元 predicate 实现相同功能: ```cpp void lambdaBasedCustomSortExample(){ std::vector<Person> people{{20}, {18}, {25}}; // Lambda表达式的写法更加灵活直观 std::sort( people.begin(), people.end(), [](const Person& a, const Person& b){return a.age<b.age;} ); } ``` 这种方法允许开发者在同一语句内清晰地描述所需的排序规则而无需提前声明辅助性的比较函数
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值