Namespaces
Variants

std::ranges::copy_backward, std::ranges::copy_backward_result

From cppreference.com
 
 
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy, ranges::sort, ...
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
(C++11)    

Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
(C++11)
(C++17)
Lexicographical comparison operations
Permutation operations


 
Constrained algorithms
All names in this menu belong to namespace std::ranges
Non-modifying sequence operations
Fold operations (Helper templates)
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
Specialized <memory> algorithms
Return types
 
Defined in header <algorithm>
Call signature
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) (since 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) (since C++20)
Helper types
template< class I1, class I2 >
using copy_backward_result = ranges::in_out_result<I1, I2>;
(3) (since C++20)

Copies all elements from the source range to the destination range ending at d_last, starting from the end and proceeding to the beginning (reverse order).

1) The source range is [firstlast).
2) The source range is r.

The function-like entities described on this page are algorithm function objects (informally known as niebloids), that is:

Parameters

first, last - the iterator-sentinel pair defining the source range
r - the source range
d_last - the past-the-end iterator of the destination range

Return value

A ranges::copy_backward_result object where:

  • The data member in holds the past-the-end iterator of the source range.
  • The data member out holds and an iterator to the last copy-assigned element in the destination range, or d_last if no element is copied.

Complexity

1) Exactly ranges::distance(first, last) assignments.
2) Exactly ranges::distance(r) assignments.

Notes

When copying overlapping ranges, ranges::copy is appropriate when copying to the left (beginning of the destination range is outside the source range) while ranges::copy_backward is appropriate when copying to the right (end of the destination range is outside the source range).

Possible implementation

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::next(ranges::begin(r), ranges::end(r)),
                       std::move(d_last));
    }
};

inline constexpr copy_backward_fn copy_backward{};

Example

#include <algorithm>
#include <iostream>
#include <ranges>
#include <string_view>
#include <vector>

void print(std::string_view rem, const std::ranges::forward_range auto& r)
{
    for (std::cout << rem << ": "; const auto& 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';
}

Output:

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

See also

copies a range of elements in backwards order
(function template) [edit]
copies a range of elements to a new location
(algorithm function object)[edit]
copies a number of elements to a new location
(algorithm function object)[edit]
copies a range of elements omitting those that satisfy specific criteria
(algorithm function object)[edit]
copies a range, replacing elements satisfying specific criteria with another value
(algorithm function object)[edit]
creates a copy of a range that is reversed
(algorithm function object)[edit]
copies and rotate a range of elements
(algorithm function object)[edit]
creates a copy of some range of elements that contains no consecutive duplicates
(algorithm function object)[edit]
moves a range of elements to a new location
(algorithm function object)[edit]
moves a range of elements to a new location in backwards order
(algorithm function object)[edit]