Namespaces
Variants

std:: reverse_copy

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
헤더 파일에 정의됨 <algorithm>
template < class BidirIt, class OutputIt >

OutputIt reverse_copy ( BidirIt first, BidirIt last,

OutputIt d_first ) ;
(1) (constexpr since C++20)
template < class ExecutionPolicy, class BidirIt, class ForwardIt >

ForwardIt reverse_copy ( ExecutionPolicy && policy,
BidirIt first, BidirIt last,

ForwardIt d_first ) ;
(2) (since C++17)
1) N std:: distance ( first, last ) 로 주어졌을 때, [ first , last ) 범위(소스 범위)의 요소들을 N 개 요소의 다른 범위로 d_first 에서 시작하여(대상 범위) 대상 범위의 요소들이 역순으로 배치되도록 복사합니다.
다음과 같이 동작합니다: 각 정수 i 에 대해 [ 0 , N ) 범위에서 다음 할당을 한 번씩 실행하는 것처럼 동작합니다: * ( d_first + N - 1 - i ) = * ( first + i ) [1] .
소스 범위와 대상 범위가 겹치는 경우, 동작은 정의되지 않습니다.
2) (1) 과 동일하지만, policy 에 따라 실행됩니다.
이 오버로드는 다음의 모든 조건이 만족될 때만 오버로드 해결에 참여합니다:

std:: is_execution_policy_v < std:: decay_t < ExecutionPolicy >> true 인 경우.

(C++20 이전)

std:: is_execution_policy_v < std:: remove_cvref_t < ExecutionPolicy >> true 인 경우.

(C++20 이후)

목차

매개변수

first, last - 복사할 요소들의 소스 범위 를 정의하는 반복자 쌍
d_first - 대상 범위의 시작 지점
타입 요구사항
-
BidirIt LegacyBidirectionalIterator 요구사항을 충족해야 함
-
OutputIt LegacyOutputIterator 요구사항을 충족해야 함
-
ForwardIt LegacyForwardIterator 요구사항을 충족해야 함

반환값

복사된 마지막 요소의 다음 위치를 가리키는 출력 반복자.

복잡도

정확히 N 개의 할당.

예외

ExecutionPolicy 라는 템플릿 매개변수를 사용하는 오버로드는 다음과 같이 오류를 보고합니다:

  • 알고리즘의 일부로 호출된 함수 실행 중 예외가 발생하고 ExecutionPolicy 표준 정책 중 하나인 경우, std::terminate 가 호출됩니다. 다른 ExecutionPolicy 의 경우 동작은 구현에 따라 정의됩니다.
  • 알고리즘이 메모리 할당에 실패하는 경우, std::bad_alloc 이 throw됩니다.

가능한 구현

다음 구현들도 참조하십시오: libstdc++ , libc++ , 그리고 MSVC STL .

template<class BidirIt, class OutputIt>
constexpr // since C++20
OutputIt reverse_copy(BidirIt first, BidirIt last, OutputIt d_first)
{
    for (; first != last; ++d_first)
        *d_first = *(--last);
    return d_first;
}

참고 사항

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

예제

#include <algorithm>
#include <iostream>
#include <vector>
int main()
{
    auto print = [](const std::vector<int>& v)
    {
        for (const auto& value : v)
            std::cout << value << ' ';
        std::cout << '\n';
    };
    std::vector<int> v{1, 2, 3};
    print(v);
    std::vector<int> destination(3);
    std::reverse_copy(std::begin(v), std::end(v), std::begin(destination));
    print(destination);
    std::reverse_copy(std::rbegin(v), std::rend(v), std::begin(destination));
    print(destination);
}

출력:

1 2 3 
3 2 1 
1 2 3

결함 보고서

다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.

DR 적용 대상 게시된 동작 올바른 동작
LWG 2074 C++98 i 에 대해 할당이
* ( d_first + N - i ) = * ( first + i ) [1]
다음으로 수정됨
* ( d_first + N - 1 - i ) = * ( first + i ) [1]
LWG 2150 C++98 단 하나의 요소만 할당이 요구됨 요구사항을 수정함
  1. 1.0 1.1 1.2 LegacyOutputIterator 는 이항 연산자 + - 를 지원할 필요가 없습니다. 여기서 + - 의 사용은 설명 전용입니다: 실제 계산에서는 이를 사용할 필요가 없습니다.

참고 항목

범위 내 요소들의 순서를 역순으로 변경
(함수 템플릿)
역순으로 된 범위의 복사본을 생성
(알고리즘 함수 객체)