std::rend

std::rend (역방향 끝 반복자)

주어진 범위·컨테이너·배열의 역방향 끝을 가리키는 반복자를 돌려주는 함수예요. C++14부터 있어요.

출처: cppreference

본문

다양한 컨테이너 헤더와 <iterator> 헤더에 정의돼 있어요.

// (1) C++14 (C++17부터 constexpr)
template< class C >
auto rend( C& c ) noexcept(noexcept(c.rend()))
    -> decltype(c.rend());

// (2) C++14 (C++17부터 constexpr)
template< class C >
auto rend( const C& c ) noexcept(noexcept(c.rend()))
    -> decltype(c.rend());

// (3) C++14 (C++17부터 constexpr, noexcept)
template< class T, std::size_t N >
std::reverse_iterator<T*> rend( T (&array)[N] ) noexcept;

// (4) C++14, crend
template< class C >
constexpr auto crend( const C& c ) noexcept(/* ... */)
    -> decltype(std::rend(c));
  • 1,2) 컨테이너 crend()를 반환해요.
    1. 배열의 첫 요소 앞을 가리키는 std::reverse_iterator<T*>를 돌려줘요.
  • crendconst 역방향 끝 반복자를 돌려줘요.

반환값

범위의 첫 요소 앞을 가리키는 역방향 반복자예요.

예제

#include <iostream>
#include <vector>
#include <iterator>

int main()
{
    std::vector<int> v = {3, 1, 4};
    // std::rbegin(v) 부터 std::rend(v) 까지 역방향 순회
    for (auto it = std::rbegin(v); it != std::rend(v); ++it)
        std::cout << *it << ' '; // "4 1 3 "
}

더 알아보기 (Learn more)

cppreference