Namespaces
Variants

std::ranges:: reverse_copy, std::ranges:: reverse_copy_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 I, std:: sentinel_for < I > S,

std:: weakly_incrementable O >
requires std:: indirectly_copyable < I, O >
constexpr reverse_copy_result < I, O >

reverse_copy ( I first, S last, O result ) ;
(1) (C++20부터)
template < ranges:: bidirectional_range R, std:: weakly_incrementable O >

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

reverse_copy ( R && r, O result ) ;
(2) (C++20부터)
헬퍼 타입
template < class I, class O >
using reverse_copy_result = ranges:: in_out_result < I, O > ;
(3) (C++20부터)
1) 소스 범위 [ first , last ) 의 요소들을 대상 범위 [ result , result + N ) 로 복사합니다. 여기서 N ranges:: distance ( first, last ) 입니다. 새로운 범위의 요소들이 역순으로 배치되도록 복사합니다. 각 정수 i 에 대해 [ 0 , N ) 범위 내에서 다음 할당을 실행하는 것처럼 동작합니다: * ( result + N - 1 - i ) = * ( first + i ) . 소스와 대상 범위가 겹치는 경우 동작은 정의되지 않습니다.
2) (1) 과 동일하지만, r 을 소스 범위로 사용하며, 마치 ranges:: begin ( r ) first 로, ranges:: end ( r ) last 로 사용하는 것과 같습니다.

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

목차

매개변수

first, last - 복사할 요소들의 소스 range 를 정의하는 iterator-sentinel 쌍
r - 복사할 요소들의 소스 범위
result - 대상 범위의 시작점

반환값

{ last, result + N } .

복잡도

정확히 N 개의 과제입니다.

참고 사항

구현체들(예: MSVC STL )은 두 반복자 타입이 모두 contiguous_iterator 를 모델링하고 동일한 값 타입을 가지며, 그 값 타입이 TriviallyCopyable 일 때 벡터화를 활성화할 수 있습니다.

가능한 구현

구현 내용은 MSVC STL libstdc++ 에서도 확인할 수 있습니다.

struct reverse_copy_fn
{
    template<std::bidirectional_iterator I, std::sentinel_for<I> S,
             std::weakly_incrementable O>
    requires std::indirectly_copyable<I, O>
    constexpr ranges::reverse_copy_result<I, O>
        operator()(I first, S last, O result) const
    {
        auto ret = ranges::next(first, last);
        for (; last != first; *result = *--last, ++result);
        return {std::move(ret), std::move(result)};
    }
    template<ranges::bidirectional_range R, std::weakly_incrementable O>
    requires std::indirectly_copyable<ranges::iterator_t<R>, O>
    constexpr ranges::reverse_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 reverse_copy_fn reverse_copy {};

예제

#include <algorithm>
#include <iostream>
#include <string>
int main()
{
    std::string x {"12345"}, y(x.size(), ' ');
    std::cout << x << " → ";
    std::ranges::reverse_copy(x.begin(), x.end(), y.begin());
    std::cout << y << " → ";
    std::ranges::reverse_copy(y, x.begin());
    std::cout << x << '\n';
}

출력:

12345 → 54321 → 12345

참고 항목

범위 내 요소들의 순서를 반전시킴
(알고리즘 함수 객체)
반전된 범위의 복사본을 생성함
(함수 템플릿)