Namespaces
Variants

std::ranges:: copy_backward, std::ranges:: copy_backward_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:: bidirectional_iterator I1, std:: sentinel_for < I1 > S1,

std:: bidirectional_iterator I2 >
requires std:: indirectly_copyable < I1, I2 >
constexpr copy_backward_result < I1, I2 >

copy_backward ( I1 first, S1 last, I2 d_last ) ;
(1) (C++20 起)
template < ranges:: bidirectional_range R, std:: bidirectional_iterator I >

requires std:: indirectly_copyable < ranges:: iterator_t < R > , I >
constexpr copy_backward_result < ranges:: borrowed_iterator_t < R > , I >

copy_backward ( R && r, I d_last ) ;
(2) (C++20 起)
辅助类型
template < class I1, class I2 >
using copy_backward_result = ranges:: in_out_result < I1, I2 > ;
(3) (C++20 起)
1) 将定义在区间 [ first , last ) 内的元素复制到另一个区间 [ d_last - N , d_last ) ,其中 N = ranges:: distance ( first, last ) 。元素按逆序复制(最后一个元素首先被复制),但它们的相对顺序保持不变。如果 d_last 位于 ( first , last ] 范围内,则行为未定义。在此情况下,可改用 std :: ranges:: copy
2) (1) 相同,但使用 r 作为源范围,如同使用 ranges:: begin ( r ) 作为 first ,并使用 ranges:: end ( r ) 作为 last

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

目录

参数

first, last - 定义待复制元素 范围 的迭代器-哨位对
r - 待复制元素的范围
d_last - 目标范围的结束位置

返回值

{ last, d_last - N }

复杂度

恰好有 N 个任务。

注释

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

可能的实现

struct copy_backward_fn
{
    template<std::bidirectional_iterator I1, std::sentinel_for<I1> S1,
             std::bidirectional_iterator I2>
    requires std::indirectly_copyable<I1, I2>
    constexpr ranges::copy_backward_result<I1, I2>
        operator()(I1 first, S1 last, I2 d_last) const
    {
        I1 last1 {ranges::next(first, std::move(last))};
        for (I1 i {last1}; i != first;)
            *--d_last = *--i;
        return {std::move(last1), std::move(d_last)};
    }
    template<ranges::bidirectional_range R, std::bidirectional_iterator I>
    requires std::indirectly_copyable<ranges::iterator_t<R>, I>
    constexpr ranges::copy_backward_result<ranges::borrowed_iterator_t<R>, I>
        operator()(R&& r, I d_last) const
    {
        return (*this)(ranges::begin(r), ranges::end(r), std::move(d_last));
    }
};
inline constexpr copy_backward_fn copy_backward{};

示例

#include <algorithm>
#include <iostream>
#include <ranges>
#include <string_view>
#include <vector>
void print(std::string_view rem, std::ranges::forward_range auto const& r)
{
    for (std::cout << rem << ": "; auto const& elem : r)
        std::cout << elem << ' ';
    std::cout << '\n';
}
int main()
{
    const auto src = {1, 2, 3, 4};
    print("src", src);
    std::vector<int> dst(src.size() + 2);
    std::ranges::copy_backward(src, dst.end());
    print("dst", dst);
    std::ranges::fill(dst, 0);
    const auto [in, out] =
        std::ranges::copy_backward(src.begin(), src.end() - 2, dst.end());
    print("dst", dst);
    std::cout
        << "(in - src.begin) == " << std::distance(src.begin(), in) << '\n'
        << "(out - dst.begin) == " << std::distance(dst.begin(), out) << '\n';
}

输出:

src: 1 2 3 4
dst: 0 0 1 2 3 4
dst: 0 0 0 0 1 2
(in - src.begin) == 2
(out - dst.begin) == 4

参见

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