Namespaces
Variants

std::ranges:: reverse

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 >

requires std:: permutable < I >
constexpr I

reverse ( I first, S last ) ;
(1) (C++20 이후)
template < ranges:: bidirectional_range R >

requires std:: permutable < ranges:: iterator_t < R >>
constexpr ranges:: borrowed_iterator_t < R >

reverse ( R && r ) ;
(2) (C++20 이후)
1) 범위 내 요소들의 순서를 역순으로 변경합니다 [ first , last ) .
모든 정수 i 에 대해 first + i, last - i - 1 반복자 쌍에 ranges::iter_swap 을 적용하는 것처럼 동작합니다. 단, 0 ≤ i < ( last - first ) / 2 입니다.
2) (1) 와 동일하지만, r 를 범위로 사용하며, 마치 ranges:: begin ( r ) first 로, ranges:: end ( r ) last 로 사용하는 것과 같습니다.

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

목차

매개변수

first, last - 역순으로 만들 요소들의 범위 를 정의하는 반복자-감시자 쌍
r - 역순으로 만들 요소들의 범위

반환값

last 와 동일한 반복자.

복잡도

정확히 ( last - first ) / 2 번의 교환.

참고 사항

구현체들(예: MSVC STL )은 반복자 타입이 contiguous_iterator 를 모델링하고, 그 값 타입의 스왑 연산이 비트리비얼 특수 멤버 함수나 ADL 에 의해 발견된 swap 을 호출하지 않을 때 벡터화를 활성화할 수 있습니다.

가능한 구현

다른 구현체는 libstdc++ MSVC STL 에서도 확인할 수 있습니다.

struct reverse_fn
{
    template<std::bidirectional_iterator I, std::sentinel_for<I> S>
    requires std::permutable<I>
    constexpr I operator()(I first, S last) const
    {
        auto last2 {ranges::next(first, last)};
        for (auto tail {last2}; !(first == tail or first == --tail); ++first)
            ranges::iter_swap(first, tail);
        return last2;
    }
    template<ranges::bidirectional_range R>
    requires std::permutable<ranges::iterator_t<R>>
    constexpr ranges::borrowed_iterator_t<R>
        operator()(R&& r) const
    {
        return (*this)(ranges::begin(r), ranges::end(r));
    }
};
inline constexpr reverse_fn reverse {};

예제

#include <algorithm>
#include <array>
#include <iostream>
#include <string>
int main()
{
    std::string s {"ABCDEF"};
    std::cout << s << " → ";
    std::ranges::reverse(s.begin(), s.end());
    std::cout << s << " → ";
    std::ranges::reverse(s);
    std::cout << s << " │ ";
    std::array a {1, 2, 3, 4, 5};
    for (auto e : a)
        std::cout << e << ' ';
    std::cout << "→ ";
    std::ranges::reverse(a);
    for (auto e : a)
        std::cout << e << ' ';
    std::cout << '\n';
}

출력:

ABCDEF → FEDCBA → ABCDEF │ 1 2 3 4 5 → 5 4 3 2 1

참고 항목

역순으로 된 범위의 복사본을 생성함
(알고리즘 함수 객체)
다른 양방향 view 의 요소들을 역순으로 순회하는 view
(클래스 템플릿) (범위 어댑터 객체)
범위 내 요소들의 순서를 역순으로 변경함
(함수 템플릿)