std::partition_copy
|   定义于头文件  <algorithm>
  | 
||
| (1) | ||
|   template< class InputIt, class OutputIt1,           class OutputIt2, class UnaryPredicate >  | 
 (C++11 起)  (C++20 前)  | 
|
|   template< class InputIt, class OutputIt1,            class OutputIt2, class UnaryPredicate >  | 
(C++20 起) | |
|   template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2,           class ForwardIt3, class UnaryPredicate >  | 
(2) | (C++17 起) | 
[first, last) 复制元素到二个不同范围,取决于谓词 p 的返回值。复制满足谓词 p 的元素到始于 d_first_true 的范围。复制剩余元素到始于 d_first_false 的范围。policy 执行。此重载仅若 std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> 为 true 才参与重载决议。目录 | 
[编辑] 参数
| first, last | - | 要排序的元素范围 | 
| d_first_true | - | 满足 p 的元素的输出范围起始 | 
| d_first_false | - | 不满足 p 的元素的输出范围起始 | 
| policy | - | 所用的执行策略。细节见执行策略。 | 
| p | - |   若元素应置于 d_first_true 中则返回 true 的一元谓词。 谓词函数签名应等价于如下者: bool pred(const Type &a); 签名不必拥有 const & ,但函数必须不修改传递给它的对象。  | 
| 类型要求 | ||
 -InputIt 必须满足  InputIterator 的要求。
 | ||
 -解引用 InputIt 结果的类型必须满足  CopyAssignable 的要求。
 | ||
 -OutputIt1, OutputIt2 必须满足  OutputIterator 的要求。
 | ||
 -ForwardIt1, ForwardIt2, ForwardIt3 必须满足  ForwardIterator 的要求。ForwardIt1 的值类型必须为可复制赋值 (CopyAssignable) ,可写入到 ForwardIt2 和 ForwardIt3 ,并且可转换为 UnaryPredicate 的参数类型
 | ||
 -UnaryPredicate 必须满足  Predicate 的要求。
 | ||
[编辑] 返回值
从指向 d_first_true 范围结尾的迭代器和指向 d_first_false 范围结尾的迭代器构造的 std::pair 。
[编辑] 复杂度
准确应用 distance(first, last) 次 p 。
对于带 ExecutionPolicy 的重载,若 ForwardIt 的值类型非可复制构造 (CopyConstructible) ,则可能有性能开销。
[编辑] 异常
拥有名为 ExecutionPolicy 的模板参数的重载按下列方式报告错误:
-  若作为算法一部分调用的函数的执行抛出异常,且 
ExecutionPolicy是三个标准策略之一,则调用 std::terminate 。对于任何其他ExecutionPolicy,行为是实现定义的。 - 若算法无法分配内存,则抛出 std::bad_alloc 。
 
[编辑] 可能的实现
template<class InputIt, class OutputIt1, class OutputIt2, class UnaryPredicate> std::pair<OutputIt1, OutputIt2> partition_copy(InputIt first, InputIt last, OutputIt1 d_first_true, OutputIt2 d_first_false, UnaryPredicate p) { while (first != last) { if (p(*first)) { *d_first_true = *first; ++d_first_true; } else { *d_first_false = *first; ++d_first_false; } ++first; } return std::pair<OutputIt1, OutputIt2>(d_first_true, d_first_false); }  | 
[编辑] 示例
#include <iostream> #include <algorithm> #include <utility> int main() { int arr [10] = {1,2,3,4,5,6,7,8,9,10}; 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 i > 5;}); std::cout << "true_arr: "; for (auto x : true_arr) { std::cout << x << ' '; } std::cout << '\n'; std::cout << "false_arr: "; for (auto x : false_arr) { std::cout << x << ' '; } std::cout << '\n'; return 0; }
输出:
true_arr: 6 7 8 9 10 false_arr: 1 2 3 4 5
[编辑] 参阅
|    把一个区间的元素分为两组   (函数模板)  | |
|    将元素分为两组,同时保留其相对顺序   (函数模板)  | |
|    (C++11)  | 
   将某一范围的元素复制到一个新的位置  (函数模板)  | 
|    复制一个范围内不满足特定条件的元素  (函数模板)  |