Namespaces
Variants

std::ranges:: copy_n, std::ranges:: copy_n_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:: weakly_incrementable O >

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

copy_n ( I first, std:: iter_difference_t < I > n, O result ) ;
(1) (C++20 起)
辅助类型
template < class I, class O >
using copy_n_result = ranges:: in_out_result < I, O > ;
(2) (C++20 起)
1) 从起始于 first 的范围精确复制 n 个值到起始于 result 的范围,对每个在 [ 0 , n ) 区间内的整数执行 * ( result + i ) = * ( first + i ) 。若 result 位于范围 [ first , first + n ) 内则行为未定义(此种情况下可改用 ranges::copy_backward )。

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

目录

参数

first - 要复制的元素范围的起始位置
n - 要复制的元素数量
result - 目标范围的起始位置

返回值

ranges :: copy_n_result { first + n, result + n } 或更正式地说,一个类型为 ranges::in_out_result 的值,其中包含一个等于 ranges:: next ( first, n ) input_iterator 迭代器,以及一个等于 ranges:: next ( result, n ) weakly_incrementable 迭代器。

复杂度

恰好 n 个赋值操作。

注释

在实践中, std::ranges::copy_n 的实现可能会避免多次赋值操作,并在值类型满足 可平凡复制 且迭代器类型满足 contiguous_iterator 时,使用批量复制函数(如 std::memmove )。此外,此类复制加速操作也可在编译器的优化阶段通过注入方式实现。

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

可能的实现

struct copy_n_fn
{
    template<std::input_iterator I, std::weakly_incrementable O>
    requires std::indirectly_copyable<I, O>
    constexpr ranges::copy_n_result<I, O>
        operator()(I first, std::iter_difference_t<I> n, O result) const
    {
        for (; n-- > 0; (void)++first, (void)++result)
            *result = *first;
        return {std::move(first), std::move(result)};
    }
};
inline constexpr copy_n_fn copy_n{};

示例

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <string>
#include <string_view>
int main()
{
    const std::string_view in{"ABCDEFGH"};
    std::string out;
    std::ranges::copy_n(in.begin(), 4, std::back_inserter(out));
    std::cout << std::quoted(out) << '\n';
    out = "abcdefgh";
    const auto res{std::ranges::copy_n(in.begin(), 5, out.begin())};
    const auto i{std::distance(std::begin(in), res.in)};
    const auto j{std::distance(std::begin(out), res.out)};
    std::cout << "in[" << i << "] = '" << in[i] << "'\n"
              << "out[" << j << "] = '" << out[j] << "'\n";
}

输出:

"ABCD"
in[5] = 'F'
out[5] = 'f'

参见

将元素范围复制到新位置
(算法函数对象)
以逆序复制元素范围
(算法函数对象)
复制元素范围并忽略满足特定条件的元素
(算法函数对象)
复制范围,并将满足特定条件的元素替换为另一个值
(算法函数对象)
创建反转后的范围副本
(算法函数对象)
复制并旋转元素范围
(算法函数对象)
创建不包含连续重复元素的范围副本
(算法函数对象)
将元素范围移动到新位置
(算法函数对象)
以逆序将元素范围移动到新位置
(算法函数对象)
(C++11)
复制指定数量的元素到新位置
(函数模板)