std::partition_copy
来自cppreference.com
| 在标头 <algorithm> 定义
|
||
| (1) | (C++11 起) (C++20 起为 constexpr) |
|
| |
(2) | (C++17 起) |
1) 根据谓词
p 的返回值,将范围 [first, last) 中的元素复制到两个不同范围。
- 满足谓词
p的元素会被复制到从d_first_true开始的范围。 - 剩余元素会被复制到从
d_first_false开始的范围。
2) 同 (1),但按照
policy 执行。 此重载只有在满足以下所有条件时才会参与重载决议:
|
|
(C++20 前) |
|
|
(C++20 起) |
如果 *first 不可写入 d_first_true 或 d_first_false,那么程序非良构。
在输入范围和两个输出范围中,如果其中两个范围有重叠,那么行为未定义。
参数
| first, last | - | 要复制的源元素范围的迭代器对 |
| d_first_true | - | 满足 p 的元素的输出范围起始
|
| d_first_false | - | 不满足 p 的元素的输出范围起始
|
| policy | - | 所用的执行策略 |
| p | - | 如果元素应置于 d_first_true 中则返回 true 的一元谓词。对每个(可为 const 的) |
| 类型要求 | ||
-InputIt 必须满足老式输入迭代器 (LegacyInputIterator) 。
| ||
-OutputIt1, OutputIt2 必须满足老式输出迭代器 (LegacyOutputIterator) 。
| ||
-ForwardIt1, ForwardIt2, ForwardIt3 必须满足老式向前迭代器 (LegacyForwardIterator) 。
| ||
-UnaryPred 必须满足谓词 (Predicate) 。
| ||
返回值
从指向 d_first_true 范围结尾的迭代器和指向 d_first_false 范围结尾的迭代器构造的 std::pair。
复杂度
准确应用 std::distance(first, last) 次 p。
对于重载 (2),如果 ForwardIt 的值类型不可复制构造 (CopyConstructible) ,那么可能会有性能开销。
异常
拥有名为 ExecutionPolicy 的模板形参的重载按下列方式报告错误:
- 如果作为算法一部分调用的函数的执行抛出异常,且
ExecutionPolicy是标准策略之一,那么调用 std::terminate。对于任何其他ExecutionPolicy,行为由实现定义。 - 如果算法无法分配内存,那么抛出 std::bad_alloc。
可能的实现
| partition_copy (1) |
|---|
template<class InputIt, class OutputIt1,
class OutputIt2, class UnaryPred>
constexpr //< C++20 起
std::pair<OutputIt1, OutputIt2>
partition_copy(InputIt first, InputIt last,
OutputIt1 d_first_true, OutputIt2 d_first_false,
UnaryPred p)
{
for (; first != last; ++first)
{
if (p(*first))
{
*d_first_true = *first;
++d_first_true;
}
else
{
*d_first_false = *first;
++d_first_false;
}
}
return std::pair<OutputIt1, OutputIt2>(d_first_true, d_first_false);
}
|
示例
运行此代码
#include <algorithm>
#include <iostream>
#include <utility>
void print(auto rem, const auto& v)
{
for (std::cout << rem; const auto& x : v)
std::cout << x << ' ';
std::cout << '\n';
}
int main()
{
int arr[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int true_arr[5] = {0};
int false_arr[5] = {0};
std::partition_copy(std::begin(arr), std::end(arr),
std::begin(true_arr), std::begin(false_arr),
[](int i) { return 4 < i; });
print("true_arr :", true_arr);
print("false_arr:", false_arr);
}
输出:
true_arr :5 6 7 8 9
false_arr:0 1 2 3 4
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| 缺陷报告 | 应用于 | 出版时的行为 | 正确行为 |
|---|---|---|---|
| P0896R4 | C++11 C++17 |
1. InputIt (C++11)/ForwardIt1(C++17)的值类型需要可复制赋值 (CopyAssignable) 2. 两个输出范围可以有重叠 |
1. 不需要 2. 此时行为未定义 |
