std::partition
来自cppreference.com
<tbody>
</tbody>
| 在标头 <algorithm> 定义
|
||
template< class ForwardIt, class UnaryPred > ForwardIt partition( ForwardIt first, ForwardIt last, UnaryPred p ); |
(1) | (C++20 起为 constexpr) |
template< class ExecutionPolicy, class ForwardIt, class UnaryPred > ForwardIt partition( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, UnaryPred p ); |
(2) | (C++17 起) |
1) 重排序范围
[first, last) 中的元素,使得谓词 p 对其返回 true 的所有元素位于谓词 p 对其返回 false 的所有元素之前。不保持相对顺序。2) 同 (1),但按照
policy 执行。 此重载只有在满足以下所有条件时才会参与重载决议:
|
|
(C++20 前) |
|
|
(C++20 起) |
如果 *first 的类型不可交换 (Swappable) (C++11 前)ForwardIt 不可交换值 (ValueSwappable) (C++11 起),那么行为未定义。
参数
| first, last | - | 要重排序的元素范围的迭代器对 |
| policy | - | 所用的执行策略 |
| p | - | 如果元素在顺序中应该在其他元素之前则返回 true 的一元谓词。对每个(可为 const 的) |
| 类型要求 | ||
-ForwardIt 必须满足老式向前迭代器 (LegacyForwardIterator) 。
| ||
-UnaryPred 必须满足谓词 (Predicate) 。
| ||
返回值
指向第二组元素中首元素的迭代器。
复杂度
给定 N 为 std::distance(first, last):
1) 应用 N 次
p。2) 应用 O(N) 次
p。 交换 O(N·log(N)) 次。
异常
拥有名为 ExecutionPolicy 的模板形参的重载按下列方式报告错误:
- 如果作为算法一部分调用的函数的执行抛出异常,且
ExecutionPolicy是标准策略之一,那么调用 std::terminate。对于任何其他ExecutionPolicy,行为由实现定义。 - 如果算法无法分配内存,那么抛出 std::bad_alloc。
可能的实现
实现重载 (1) 并保留 C++11 兼容性。
template<class ForwardIt, class UnaryPred>
ForwardIt partition(ForwardIt first, ForwardIt last, UnaryPred p)
{
first = std::find_if_not(first, last, p);
if (first == last)
return first;
for (auto i = std::next(first); i != last; ++i)
{
if (p(*i))
{
std::iter_swap(i, first);
++first;
}
}
return first;
}
|
示例
运行此代码
#include <algorithm>
#include <forward_list>
#include <iostream>
#include <iterator>
#include <vector>
template <class ForwardIt>
void quicksort(ForwardIt first, ForwardIt last)
{
if (first == last)
return;
auto pivot = *std::next(first, std::distance(first, last) / 2);
auto middle1 = std::partition(first, last, [pivot](const auto& em)
{
return em < pivot;
});
auto middle2 = std::partition(middle1, last, [pivot](const auto& em)
{
return !(pivot < em);
});
quicksort(first, middle1);
quicksort(middle2, last);
}
int main()
{
std::vector<int> v{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
std::cout << "原来的 vector:\n ";
for (int elem : v)
std::cout << elem << ' ';
auto it = std::partition(v.begin(), v.end(), [](int i) {return i % 2 == 0;});
std::cout << "\n划分后的 vector:\n ";
std::copy(std::begin(v), it, std::ostream_iterator<int>(std::cout, " "));
std::cout << "* ";
std::copy(it, std::end(v), std::ostream_iterator<int>(std::cout, " "));
std::forward_list<int> fl = {1, 30, -4, 3, 5, -4, 1, 6, -8, 2, -5, 64, 1, 92};
std::cout << "\n未排序的列表:\n ";
for (int n : fl)
std::cout << n << ' ';
quicksort(std::begin(fl), std::end(fl));
std::cout << "\n用 quicksort 排序后:\n ";
for (int fi : fl)
std::cout << fi << ' ';
std::cout << '\n';
}
可能的输出:
原来的 vector:
0 1 2 3 4 5 6 7 8 9
划分后的 vector:
0 8 2 6 4 * 5 3 7 1 9
未排序的列表:
1 30 -4 3 5 -4 1 6 -8 2 -5 64 1 92
快速排序后:
-8 -5 -4 -4 1 1 1 2 3 5 6 30 64 92
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| 缺陷报告 | 应用于 | 出版时的行为 | 正确行为 |
|---|---|---|---|
| LWG 498 | C++98 | std::partition 要求 first 和 last 是老式双向迭代器 (LegacyBidirectionalIterator) |
只需要是 老式向前迭代器 (LegacyForwardIterator) |
| LWG 2150 | C++98 | std::partition 只需要将一个满足 p的元素放在一个不满足 p 的元素之前
|
改正要求 |
参阅
(C++11) |
判断范围是否已按给定谓词划分 (函数模板) |
| 将元素分为两组,同时保留其相对顺序 (函数模板) | |
(C++20) |
将范围中元素分为两组 (算法函数对象) |