std::stable_sort
受约束算法及范围上的算法 (C++20) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
受约束算法: std::ranges::copy, std::ranges::sort, ... | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
执行策略 (C++17) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
不修改序列的操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
修改序列的操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Partitioning operations | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
划分操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
排序操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
二分搜索操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
集合操作(在已排序范围上) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
堆操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
最小/最大操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
排列 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
数值运算 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
未初始化存储上的操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
C 库 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
在标头
<algorithm>
定义
|
||
(1) | ||
template< class RandomIt >
void stable_sort( RandomIt first, RandomIt last ); |
(C++26 前) | |
template< class RandomIt >
constexpr void stable_sort( RandomIt first, RandomIt last ); |
(C++26 起) | |
template< class ExecutionPolicy,
class RandomIt >
void stable_sort(
ExecutionPolicy&& policy, |
(2) | (C++17 起) |
(3) | ||
template< class RandomIt, class Compare >
void stable_sort( RandomIt first, RandomIt last, Compare comp ); |
(C++26 前) | |
template< class RandomIt, class Compare >
constexpr void stable_sort( RandomIt first, RandomIt last, Compare comp ); |
(C++26 起) | |
template< class ExecutionPolicy,
class RandomIt, class
Compare >
void stable_sort(
ExecutionPolicy&& policy, |
(4) | (C++17 起) |
以不降序排序范围 [first, last)
中的元素。保证保持等价元素的顺序。
若对于任何指向序列的迭代器 it
与任何使得 it + n
为指向序列元素的合法迭代器的非负整数 n
, comp(*(it + n), *it) 求值为 false
,则称序列相对于 comp
已排序。
operator<
比较元素。comp
比较元素。policy
执行。这些重载只有在
std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> |
(C++20 前) |
std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> |
(C++20 起) |
参数
first, last | - | 要排序的元素范围 |
policy | - | 所用的执行策略。细节见执行策略。 |
comp | - | 比较函数对象(即满足比较 (Compare)
概念的对象),在第一参数小于(即先 序于)第二参数时返回 true。 比较函数的签名应等价于如下: bool cmp(const Type1 &a, const Type2 &b); 虽然签名不必有 |
类型要求 | ||
-RandomIt 必须符合值可交换 (ValueSwappable)
和 老式随机访问迭代器 (LegacyRandomAccessIterator)
的要求。
|
||
-解引用 RandomIt 结果的类型必须满足可移动赋值 (MoveAssignable)
和可移动构造
(MoveConstructible)
的要求。
|
返回值
(无)
复杂度
应用 cmp
O(N·log(N)2) 次,其中 N = std::distance(first,
last) 。若额外内存可用,则复杂度为 O(N·log(N)) 。
异常
拥有名为 ExecutionPolicy
的模板形参的重载按下列方式报告错误:
- 如果作为算法一部分调用的函数的执行抛出异常,且
ExecutionPolicy
是标准策略之一,那么调用 std::terminate。对于任何其他ExecutionPolicy
,行为由实现定义。 - 如果算法无法分配内存,那么抛出 std::bad_alloc。
注解
此函数试图分配等于待排序序列长度的临时缓冲区。若分配失败,则选择较低效的算法。
功能特性测试宏 | 值 | 备注 |
---|---|---|
__cpp_lib_constexpr_algorithms |
202306L | constexpr 的稳定排序
|
可能的实现
示例
#include <algorithm> #include <iostream> #include <string> #include <vector> struct Employee { int age; std::string name; // 不参与比较 }; bool operator<(const Employee &lhs, const Employee &rhs) { return lhs.age < rhs.age; } int main() { std::vector<Employee> v = { {108, "Zaphod"}, {32, "Arthur"}, {108, "Ford"}, }; std::stable_sort(v.begin(), v.end()); for (const auto &e : v) std::cout << e.age << ", " << e.name << '\n'; }
输出:
32, Arthur 108, Zaphod 108, Ford
参阅
排序一个范围的前 N 个元素 (函数模板) |
|
将范围按升序排序 (函数模板) |