Namespaces
Variants

std::ranges:: copy, std::ranges:: copy_if, std::ranges:: copy_result, std::ranges:: copy_if_result

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
(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
Constrained algorithms
All names in this menu belong to namespace std::ranges
Non-modifying sequence operations
Modifying sequence operations
Partitioning operations
Sorting operations
Binary search operations (on sorted ranges)
Set operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Permutation operations
Fold operations
Operations on uninitialized storage
Return types
定义于头文件 <algorithm>
调用签名
template < std:: input_iterator I, std:: sentinel_for < I > S, std:: weakly_incrementable O >

requires std:: indirectly_copyable < I, O >
constexpr copy_result < I, O >

copy ( I first, S last, O result ) ;
(1) (C++20 起)
template < ranges:: input_range R, std:: weakly_incrementable O >

requires std:: indirectly_copyable < ranges:: iterator_t < R > , O >
constexpr copy_result < ranges:: borrowed_iterator_t < R > , O >

copy ( R && r, O result ) ;
(2) (C++20 起)
template < std:: input_iterator I, std:: sentinel_for < I > S, std:: weakly_incrementable O,

class Proj = std:: identity ,
std:: indirect_unary_predicate < std :: projected < I, Proj >> Pred >
requires std:: indirectly_copyable < I, O >
constexpr copy_if_result < I, O >

copy_if ( I first, S last, O result, Pred pred, Proj proj = { } ) ;
(3) (C++20 起)
template < ranges:: input_range R, std:: weakly_incrementable O,

class Proj = std:: identity ,
std:: indirect_unary_predicate <
std :: projected < ranges:: iterator_t < R > , Proj >> Pred >
requires std:: indirectly_copyable < ranges:: iterator_t < R > , O >
constexpr copy_if_result < ranges:: borrowed_iterator_t < R > , O >

copy_if ( R && r, O result, Pred pred, Proj proj = { } ) ;
(4) (C++20 起)
辅助类型
template < class I, class O >
using copy_result = ranges:: in_out_result < I, O > ;
(5) (C++20 起)
template < class I, class O >
using copy_if_result = ranges:: in_out_result < I, O > ;
(6) (C++20 起)

将区间 [ first , last ) 内定义的元素复制到以 result 起始的另一区间。

1) 复制范围 [ first , last ) 中的所有元素,从 first 开始并持续到 last - 1 。如果 result 位于范围 [ first , last ) 内,则行为未定义。在这种情况下,可改用 ranges::copy_backward
3) 仅复制谓词 pred 返回 true 的元素。被复制元素的相对顺序保持不变。若源范围与目标范围重叠,则行为未定义。
2,4) (1,3) 相同,但使用 r 作为源范围,如同以 ranges:: begin ( r ) 作为 first ,并以 ranges:: end ( r ) 作为 last

本页面描述的函数式实体是 算法函数对象 (非正式称为 niebloids ),即:

目录

参数

first, last - 定义待复制元素 范围 的迭代器-哨位对
r - 待复制元素的范围
result - 目标范围的起始位置
pred - 应用于投影元素的谓词
proj - 应用于元素的投影

返回值

一个 ranges::in_out_result ,包含一个等于 last 的输入迭代器和一个指向最后被复制元素之后位置的输出迭代器。

复杂度

1,2) 精确进行 last - first 次赋值。
3,4) 精确执行 last - first 次谓词和投影操作,进行 0 last - first 次赋值(对每个谓词返回 true 的元素执行赋值,具体次数取决于谓词和输入数据)。

注释

在实际应用中, ranges::copy 的实现会避免多次赋值操作,当值类型满足 TriviallyCopyable 且迭代器类型满足 contiguous_iterator 时,会使用批量拷贝函数(如 std::memmove )。

当复制重叠范围时,若向左复制(目标范围的起始位置位于源范围之外),适合使用 ranges::copy ;而若向右复制(目标范围的结束位置位于源范围之外),则适合使用 ranges::copy_backward

可能的实现

copy (1)(2)
struct copy_fn
{
    template<std::input_iterator I, std::sentinel_for<I> S, std::weakly_incrementable O>
    requires std::indirectly_copyable<I, O>
    constexpr ranges::copy_result<I, O> operator()(I first, S last, O result) const
    {
        for (; first != last; ++first, (void)++result)
            *result = *first;
        return {std::move(first), std::move(result)};
    }
    template<ranges::input_range R, std::weakly_incrementable O>
    requires std::indirectly_copyable<ranges::iterator_t<R>, O>
    constexpr ranges::copy_result<ranges::borrowed_iterator_t<R>, O>
        operator()(R&& r, O result) const
    {
        return (*this)(ranges::begin(r), ranges::end(r), std::move(result));
    }
};
inline constexpr copy_fn copy;
copy_if (3)(4)
struct copy_if_fn
{
    template<std::input_iterator I, std::sentinel_for<I> S, std::weakly_incrementable O,
             class Proj = std::identity,
             std::indirect_unary_predicate<std::projected<I, Proj>> Pred>
    requires std::indirectly_copyable<I, O>
    constexpr ranges::copy_if_result<I, O>
        operator()(I first, S last, O result, Pred pred, Proj proj = {}) const
    {
        for (; first != last; ++first)
            if (std::invoke(pred, std::invoke(proj, *first)))
            {
                *result = *first;
                ++result;
            }
        return {std::move(first), std::move(result)};
    }
    template<ranges::input_range R, std::weakly_incrementable O,
             class Proj = std::identity,
             std::indirect_unary_predicate<
                 std::projected<ranges::iterator_t<R>, Proj>> Pred>
    requires std::indirectly_copyable<ranges::iterator_t<R>, O>
    constexpr ranges::copy_if_result<ranges::borrowed_iterator_t<R>, O>
        operator()(R&& r, O result, Pred pred, Proj proj = {}) const
    {
        return (*this)(ranges::begin(r), ranges::end(r), std::move(result),
                       std::ref(pred), std::ref(proj));
    }
};
inline constexpr copy_if_fn copy_if;

示例

以下代码使用 ranges::copy 将一个 std::vector 的内容复制到另一个容器,并显示生成的 std::vector

#include <algorithm>
#include <iostream>
#include <iterator>
#include <numeric>
#include <vector>
int main()
{
    std::vector<int> source(10);
    std::iota(source.begin(), source.end(), 0);
    std::vector<int> destination;
    std::ranges::copy(source.begin(), source.end(), std::back_inserter(destination));
// 或者也可以这样写:
//  std::vector<int> destination(source.size());
//  std::ranges::copy(source.begin(), source.end(), destination.begin());
// 两种写法都等价于:
//  std::vector<int> destination = source;
    std::cout << "Destination contains: ";
    std::ranges::copy(destination, std::ostream_iterator<int>(std::cout, " "));
    std::cout << '\n';
    std::cout << "Odd numbers in destination are: ";
    std::ranges::copy_if(destination, std::ostream_iterator<int>(std::cout, " "),
                         [](int x) { return (x % 2) == 1; });
    std::cout << '\n';
}

输出:

Destination contains: 0 1 2 3 4 5 6 7 8 9
Odd numbers in destination are: 1 3 5 7 9

参见

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