std::vector<T,Allocator>::rbegin, std::vector<T,Allocator>::crbegin
출처: ko.cppreference.net
reverse_iterator rbegin();
|
(1) | (C++11부터 noexcept) (C++20부터 constexpr) |
const_reverse_iterator rbegin() const;
|
(2) | (C++11부터 noexcept) (C++20부터 constexpr) |
const_reverse_iterator crbegin() const noexcept;
|
(3) | (C++11부터) (C++20부터 constexpr) |
역순 vector의 첫 번째 요소를 가리키는 역방향 반복자를 반환합니다. 이는 역순이 아닌 vector의 마지막 요소에 해당합니다. vector가 비어 있는 경우, 반환된 반복자는 rend()와 같습니다.
반환값
첫 번째 요소로의 역방향 반복자.
복잡도
상수.
참고 사항
반환된 역방향 반복자의 기반 반복자 는 end iterator 입니다. 따라서 end iterator가 무효화될 때 반환된 반복자도 함께 무효화됩니다.
libc++ 백포트가
crbegin()
를 C++98 모드로 이식합니다.
예제
이 코드 실행
#include <algorithm>
#include <cassert>
#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;
// Print vector nums.
std::for_each(nums.crbegin(), nums.crend(),
[](const int n) { std::cout << n << ' '; });
std::cout << '\n';
// Sum all integers in the vector nums, printing only the result.
std::cout << "Sum of nums: "
<< std::accumulate(nums.crbegin(), nums.crend(), 0) << '\n';
// Print the last fruit in the vector fruits, checking if there is any.
if (!fruits.empty())
std::cout << "Last fruit: " << *fruits.crbegin() << '\n';
if (empty.crbegin() == empty.crend())
std::cout << "vector ‘empty’ is indeed empty.\n";
// Modify the last element in nums.
*nums.rbegin() = 32;
assert(*nums.crbegin() == 32);
}
출력:
16 8 4 2 1
Sum of nums: 31
Last fruit: raspberry
vector ‘empty’ is indeed empty.
참고 항목
|
(C++11)
|
역방향 반복자를 끝 위치로 반환
(public member function) |
|
(C++14)
|
컨테이너나 배열의 시작 위치로 역방향 반복자를 반환
(function template) |