Namespaces
Variants

std:: copy, std:: copy_if

From cppreference.net
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy , ranges::sort , ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
Modifying sequence operations
Copy operations
copy copy_if
(C++11)
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17) (C++11)
(C++20) (C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
定义于头文件 <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 起始的目标区间(复制目标区间)。

1) 复制区间 [ first , last ) 内的所有元素,从 first 开始并持续到 last
如果 d_first 位于 [ first , last ) 范围内,则行为未定义。此时可改用 std::copy_backward
2) 复制元素,但根据 policy 执行。
此重载仅当满足以下所有条件时才参与重载决议:

std:: is_execution_policy_v < std:: decay_t < ExecutionPolicy >> true

(C++20 前)

std:: is_execution_policy_v < std:: remove_cvref_t < ExecutionPolicy >> true

(C++20 起)
如果 [ first , last ) 与复制目标范围重叠,则行为未定义。
3) 仅复制谓词 pred 返回 true 的元素。该复制算法是稳定的:被复制元素的相对顺序保持不变。
如果 [ first , last ) 与复制目标范围重叠,则行为未定义。
4) (3) 相同,但根据 policy 执行。
此重载仅在满足以下所有条件时参与重载决议:

std:: is_execution_policy_v < std:: decay_t < ExecutionPolicy >> true

(C++20 前)

std:: is_execution_policy_v < std:: remove_cvref_t < ExecutionPolicy >> true

(C++20 起)

目录

参数

first, last - 定义待复制元素源 范围 的迭代器对
d_first - 目标范围的起始位置
policy - 使用的 执行策略
pred - 返回所需元素为​ true 的一元谓词

表达式 pred ( v ) 必须可转换为 bool ,其中参数 v 为类型(可能为 const) VT VT InputIt 的值类型),且与 值类别 无关,同时不得修改 v 。因此不允许使用 VT & 作为参数类型 ,也不允许使用 VT ,除非对于 VT 类型移动操作等同于复制操作 (C++11 起) 。 ​

类型要求
-
InputIt 必须满足 LegacyInputIterator 的要求。
-
OutputIt 必须满足 LegacyOutputIterator 的要求。
-
ForwardIt1, ForwardIt2 必须满足 LegacyForwardIterator 的要求。
-
UnaryPred 必须满足 Predicate 的要求。

返回值

指向目标范围中元素的输出迭代器,位于最后被复制元素的下一个位置。

复杂度

给定 N std:: distance ( first, last )

1,2) 恰好进行 N 次赋值。
3,4) 恰好应用谓词 pred N 次,至多进行 N 次赋值。

对于带有 ExecutionPolicy 的重载,如果 ForwardIt1 的值类型不满足 可移动构造 要求,则可能会产生性能开销。

异常

带有名为 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;
}

注释

在实践中, 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 contains: ";
    std::copy(to_vector.begin(), to_vector.end(),
              std::ostream_iterator<int>(std::cout, " "));
    std::cout << '\n';
    std::cout << "odd numbers in to_vector are: ";
    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 contains these multiples of 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 (const int x : to_vector)
        std::cout << x << ' ';
    std::cout << '\n';
}

可能的输出:

to_vector contains: 0 1 2 3 4 5 6 7 8 9
odd numbers in to_vector are: 1 3 5 7 9
to_vector contains these multiples of 3: 0 3 6 9

缺陷报告

下列行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。

缺陷报告 适用标准 发布时行为 正确行为
LWG 2039 C++11 未指定 std::copy_if 的返回值 已指定
LWG 2044 C++11 未定义 std::copy_if 的稳定性 已定义

参见

以逆序复制一系列元素
(函数模板)
创建反转范围的副本
(函数模板)
(C++11)
复制指定数量的元素到新位置
(函数模板)
将给定值复制赋值给范围内的每个元素
(函数模板)
复制范围元素并忽略满足特定条件的元素
(函数模板)
复制一系列元素到新位置
(算法函数对象)