Namespaces
Variants

std::list<T,Allocator>:: rbegin, std::list<T,Allocator>:: crbegin

From cppreference.net

reverse_iterator rbegin ( ) ;
(1) (C++11부터 noexcept)
(C++26부터 constexpr)
const_reverse_iterator rbegin ( ) const ;
(2) (C++11부터 noexcept)
(C++26부터 constexpr)
const_reverse_iterator crbegin ( ) const noexcept ;
(3) (C++11부터)
(C++26부터 constexpr)

역순으로 바뀐 * this 의 첫 번째 요소를 가리키는 reverse iterator를 반환합니다. 이는 역순으로 바뀌지 않은 * this 의 마지막 요소에 해당합니다.

만약 * this 가 비어 있으면, 반환된 반복자는 rend() 와 같습니다.

range-rbegin-rend.svg

목차

반환값

첫 번째 요소로의 역방향 반복자.

복잡도

상수.

참고 사항

반환된 역방향 반복자의 기본 반복자 end iterator 입니다. 따라서 반환된 반복자는 end iterator가 무효화될 때 함께 무효화됩니다.

libc++ 백포트가 crbegin() 를 C++98 모드로 이전합니다.

예제

#include <algorithm>
#include <iostream>
#include <numeric>
#include <string>
#include <list>
int main()
{
    std::list<int> nums{1, 2, 4, 8, 16};
    std::list<std::string> fruits{"orange", "apple", "raspberry"};
    std::list<char> empty;
    // 리스트 출력
    std::for_each(nums.rbegin(), nums.rend(), [](const int n) { std::cout << n << ' '; });
    std::cout << '\n';
    // 리스트 nums의 모든 정수를 합산하여 결과만 출력
    std::cout << "Sum of nums: "
              << std::accumulate(nums.rbegin(), nums.rend(), 0) << '\n';
    // 리스트 fruits의 첫 번째 과일을 출력하며, 존재 여부 확인
    if (!fruits.empty())
        std::cout << "First fruit: " << *fruits.rbegin() << '\n';
    if (empty.rbegin() == empty.rend())
        std::cout << "list 'empty' is indeed empty.\n";
}

출력:

16 8 4 2 1
Sum of nums: 31
First fruit: raspberry
list 'empty' is indeed empty.

참고 항목

(C++11)
역방향 반복자를 끝 위치로 반환
(public member function)
컨테이너나 배열의 시작 위치로 역방향 반복자를 반환
(function template)