std::list<T,Allocator>::rbegin, std::list<T,Allocator>::crbegin - cppreference.com

From cppreference.com

reverse_iteratorrbegin(); (1) (noexcept since C++11)const_reverse_iteratorrbegin()const; (2) (noexcept since C++11)const_reverse_iteratorcrbegin()constnoexcept; (3) (since C++11)Returns a reverse iterator to the first element of the reversed list. It corresponds to the last element of the non-reversed list. If the list is empty, the returned iterator is equal to

rend()

.

Return value

Reverse iterator to the first element.

Complexity

Constant.

Notes

The

underlying iterator

of the returned reverse iterator is the

end iterator

. Hence the returned iterator is invalidated if and when the end iterator is invalidated.

libc++ backports crbegin() to C++98 mode.

Example

Run this code

#include<algorithm>#include<cassert>#include<iostream>#include<numeric>#include<string>#include<list>intmain(){std::list<int>nums{1,2,4,8,16};std::list<std::string>fruits{"orange","apple","raspberry"};std::list<char>empty;// Print list nums.std::for_each(nums.crbegin(),nums.crend(),[](constintn){std::cout<<n<<' ';});std::cout<<'\n';// Sum all integers in the list nums, printing only the result.std::cout<<"Sum of nums: "<<std::accumulate(nums.crbegin(),nums.crend(),0)<<'\n';// Print the last fruit in the list fruits, checking if there is any.if(!fruits.empty())std::cout<<"Last fruit: "<<*fruits.crbegin()<<'\n';if(empty.crbegin()==empty.crend())std::cout<<"list ‘empty’ is indeed empty.\n";// Modify the last element in nums.*nums.rbegin()=32;assert(*nums.crbegin()==32);}Output:

16 8 4 2 1 Sum of nums: 31 Last fruit: raspberry list ‘empty’ is indeed empty. See also

(C++11)

returns a reverse iterator to the end
(public member function)

[edit]

(C++14)

returns a reverse iterator to the beginning of a container or array
(function template)

[edit]