std::copy, std::copy_if
|   在标头  <algorithm> 定义
  | 
||
|   template< class InputIt, class OutputIt > OutputIt copy( InputIt first, InputIt last,  | 
(1) |  (C++20 起为 constexpr)  | 
|   template< class ExecutionPolicy,           class ForwardIt1, class ForwardIt2 >  | 
(2) | (C++17 起) | 
|   template< class InputIt, class OutputIt, class UnaryPred > OutputIt copy_if( InputIt first, InputIt last,  | 
(3) |  (C++11 起)  (C++20 起为 constexpr)  | 
|   template< class ExecutionPolicy,           class ForwardIt1, class ForwardIt2, class UnaryPred >  | 
(4) | (C++17 起) | 
复制范围 [first, last) 中的元素到从 d_first 开始的另一范围(复制目标范围)。
[first, last) 中的所有元素。| 
 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) 与复制目标范围重叠,那么行为未定义。[first, last) 与复制目标范围重叠,那么行为未定义。| 
 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 | - | 要复制的元素范围 | 
| d_first | - | 目标范围的起始 | 
| policy | - | 所用的执行策略。细节见执行策略。 | 
| pred | - |  对所要求的元素则返回 true 的一元谓词。 对每个(可为 const 的)   | 
| 类型要求 | ||
 -InputIt 必须满足老式输入迭代器 (LegacyInputIterator) 。
 | ||
 -OutputIt 必须满足老式输出迭代器 (LegacyOutputIterator) 。
 | ||
 -ForwardIt1, ForwardIt2 必须满足老式向前迭代器 (LegacyForwardIterator) 。
 | ||
 -UnaryPred 必须满足谓词 (Predicate) 。
 | ||
[编辑] 返回值
指向目标范围中最后复制元素的下个元素的输出迭代器。
[编辑] 复杂度
给定 N 为 std::distance(first, last):
对于带 ExecutionPolicy 的重载,如果 ForwardIt1 的值类型不可移动构造 (MoveConstructible) ,那么就会有性能开销。
[编辑] 异常
拥有名为 ExecutionPolicy 的模板形参的重载按下列方式报告错误:
-  如果作为算法一部分调用的函数的执行抛出异常,且 
ExecutionPolicy是标准策略之一,那么调用 std::terminate。对于任何其他ExecutionPolicy,行为由实现定义。 - 如果算法无法分配内存,那么抛出 std::bad_alloc。
 
[编辑] 可能的实现
| copy (1) | 
|---|
template<class InputIt, class OutputIt> OutputIt copy(InputIt first, InputIt last, OutputIt d_first) { for (; first != last; (void)++first, (void)++d_first) *d_first = *first; return d_first; }  | 
| copy_if (3) | 
template<class InputIt, class OutputIt, class UnaryPred> OutputIt copy_if(InputIt first, InputIt last, OutputIt d_first, UnaryPred pred) { for (; first != last; ++first) if (pred(*first)) { *d_first = *first; ++d_first; } return d_first; }  | 
[编辑] 注解
实践中,如果值类型可平凡复制 (TriviallyCopyable) ,而迭代器类型满足老式连续迭代器 (LegacyContiguousIterator) ,那么 std::copy 的实现会避免多次赋值并使用大批量复制函数,如 std::memmove。
复制重叠的范围时,std::copy 在复制到左侧(目标范围起始在源范围之外)时适合,而 std::copy_backward 在复制到右侧(目标范围结尾在源范围之外)时适合。
[编辑] 示例
下列代码用 std::copy 复制一个 std::vector 的内容到另一个,并显示结果 std::vector。
#include <algorithm> #include <iostream> #include <iterator> #include <numeric> #include <vector> int main() { std::vector<int> from_vector(10); std::iota(from_vector.begin(), from_vector.end(), 0); std::vector<int> to_vector; std::copy(from_vector.begin(), from_vector.end(), std::back_inserter(to_vector)); // 或者可以使用另一种方式, // std::vector<int> to_vector(from_vector.size()); // std::copy(from_vector.begin(), from_vector.end(), to_vector.begin()); // 任一方式都等价于 // std::vector<int> to_vector = from_vector; std::cout << "to_vector 包含:"; std::copy(to_vector.begin(), to_vector.end(), std::ostream_iterator<int>(std::cout, " ")); std::cout << '\n'; std::cout << "to_vector 中的奇数有:"; std::copy_if(to_vector.begin(), to_vector.end(), std::ostream_iterator<int>(std::cout, " "), [](int x) { return x % 2 != 0; }); std::cout << '\n'; std::cout << "to_vector 中 3 的倍数有:"; to_vector.clear(); std::copy_if(from_vector.begin(), from_vector.end(), std::back_inserter(to_vector), [](int x) { return x % 3 == 0; }); for (int x : to_vector) std::cout << x << ' '; std::cout << '\n'; }
可能的输出:
to_vector 包含:0 1 2 3 4 5 6 7 8 9 to_vector 中的奇数有:1 3 5 7 9 to_vector 中 3 的倍数有:0 3 6 9
[编辑] 缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| 缺陷报告 | 应用于 | 出版时的行为 | 正确行为 | 
|---|---|---|---|
| LWG 2039 | C++11 |  未指定 std::copy_if 的返回值
 | 
已指定 | 
| LWG 2044 | C++11 |  未定义 std::copy_if 的稳定性
 | 
已定义 | 
[编辑] 参阅
|    按从后往前的顺序复制一个范围内的元素  (函数模板)  | |
|    创建一个范围的逆向副本   (函数模板)  | |
|    (C++11)  | 
   将一定数目的元素复制到一个新的位置  (函数模板)  | 
|    将一个给定值复制赋值给一个范围内的每个元素   (函数模板)  | |
|    复制一个范围的元素,忽略满足特定判别标准的元素  (函数模板)  | |
|    (C++20)(C++20)  | 
  将某一范围的元素复制到一个新的位置   (niebloid)  |