std::copy, std::copy_if
| 在标头 <algorithm> 定义
|
||
template< class InputIt, class OutputIt > OutputIt copy( InputIt first, InputIt last, OutputIt d_first ); |
(1) | (C++20 起为 constexpr) |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 > ForwardIt2 copy( ExecutionPolicy&& policy, ForwardIt1 first, ForwardIt1 last, ForwardIt2 d_first ); |
(2) | (C++17 起) |
template< class InputIt, class OutputIt, class UnaryPred > OutputIt copy_if( InputIt first, InputIt last, OutputIt d_first, UnaryPred pred ); |
(3) | (C++11 起) (C++20 起为 constexpr) |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class UnaryPred > ForwardIt2 copy_if( ExecutionPolicy&& policy, ForwardIt1 first, ForwardIt1 last, ForwardIt2 d_first, UnaryPred pred ); |
(4) | (C++17 起) |
复制范围 [first, last) 中的元素到从 d_first 开始的另一范围(复制目标范围)。
first 到 last 的顺序复制 [first, last) 中的所有元素。d_first 在 [first, last) 中,那么行为未定义。此时可以用 std::copy_backward 代替。policy 执行。此重载只有在满足以下所有条件时才会参与重载决议:
|
|
(C++20 前) |
|
|
(C++20 起) |
[first, last) 与复制目标范围重叠,那么行为未定义。pred 对其返回 true 的元素。此复制算法是稳定的:保持被复制元素的相对顺序。[first, last) 与复制目标范围重叠,那么行为未定义。policy 执行。此重载只有在满足以下所有条件时才会参与重载决议:
|
|
(C++20 前) |
|
|
(C++20 起) |
参数
| first, last | - | 要复制的源元素范围的迭代器对 |
| d_first | - | 目标范围的起始 |
| policy | - | 所用的执行策略 |
| pred | - | 对所要求的元素则返回 true 的一元谓词。对每个(可为 const 的) |
| 类型要求 | ||
-InputIt 必须满足老式输入迭代器 (LegacyInputIterator) 。
| ||
-OutputIt 必须满足老式输出迭代器 (LegacyOutputIterator) 。
| ||
-ForwardIt1, ForwardIt2 必须满足老式向前迭代器 (LegacyForwardIterator) 。
| ||
-UnaryPred 必须满足谓词 (Predicate) 。
| ||
返回值
指向目标范围中最后复制元素的下个元素的输出迭代器。
复杂度
给定 N 为 std::distance(first, last):
pred,并且赋值最多 N 次。对于带 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) |
复制范围中元素到新位置 (算法函数对象) |