Namespaces
Variants

std::vector<T,Allocator>:: rend, std::vector<T,Allocator>:: crend

From cppreference.net

reverse_iterator rend ( ) ;
(1) (C++11부터 noexcept)
(C++20부터 constexpr)
const_reverse_iterator rend ( ) const ;
(2) (C++11부터 noexcept)
(C++20부터 constexpr)
const_reverse_iterator crend ( ) const noexcept ;
(3) (C++11부터)
(C++20부터 constexpr)

역방향으로 된 * this 의 마지막 요소 바로 다음을 가리키는 역방향 반복자를 반환합니다. 이는 비역방향 * this 의 첫 번째 요소 바로 앞에 해당하는 요소와 일치합니다.

이 반환된 반복자는 단지 센티널 역할만 합니다. 이것이 dereferenceable 하다는 보장은 없습니다.

range-rbegin-rend.svg

목차

반환값

마지막 요소 다음에 오는 요소로의 역방향 반복자.

복잡도

상수.

참고 사항

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

예제

#include <algorithm>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
int main()
{
    std::vector<int> nums{1, 2, 4, 8, 16};
    std::vector<std::string> fruits{"orange", "apple", "raspberry"};
    std::vector<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 << "vector 'empty' is indeed empty.\n";
}

출력:

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

참고 항목

시작 부분을 가리키는 역방향 반복자를 반환합니다
(public member function)
(C++14)
컨테이너나 배열의 역방향 끝 반복자를 반환합니다
(function template)