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 로 사용하는 것과 같습니다.

이 페이지에서 설명하는 함수형 개체들은 algorithm function objects (일반적으로 niebloids 로 알려진)입니다. 즉:

목차

매개변수

first, last - 복사할 요소들의 범위 를 정의하는 iterator-sentinel 쌍
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

참고 항목

요소 범위를 새 위치로 복사합니다
(알고리즘 함수 객체)
지정된 개수의 요소를 새 위치로 복사합니다
(알고리즘 함수 객체)
특정 조건을 만족하는 요소를 제외하고 요소 범위를 복사합니다
(알고리즘 함수 객체)
특정 조건을 만족하는 요소를 다른 값으로 대체하여 범위를 복사합니다
(알고리즘 함수 객체)
역순으로 된 범위의 복사본을 생성합니다
(알고리즘 함수 객체)
요소 범위를 복사하고 회전합니다
(알고리즘 함수 객체)
연속된 중복 요소가 없는 일부 범위의 복사본을 생성합니다
(알고리즘 함수 객체)
요소 범위를 새 위치로 이동합니다
(알고리즘 함수 객체)
요소 범위를 역순으로 새 위치로 이동합니다
(알고리즘 함수 객체)
요소 범위를 역순으로 복사합니다
(함수 템플릿)