std::rbegin, std::crbegin - cppreference.com

From cppreference.com

Defined in header

<array>

Defined in header

<deque>

Defined in header

<flat_map>

(since C++23)

Defined in header

<flat_set>

(since C++23)

Defined in header

<forward_list>

Defined in header

<hive>

(since C++26)

Defined in header

<inplace_vector>

(since C++26)

Defined in header

<iterator>

Defined in header

<list>

Defined in header

<map>

Defined in header

<optional>

(since C++26)

Defined in header

<regex>

Defined in header

<set>

Defined in header

<span>

(since C++20)

Defined in header

<stacktrace>

(since C++23)

Defined in header

<string>

Defined in header

<string_view>

(since C++17)

Defined in header

<unordered_map>

Defined in header

<unordered_set>

Defined in header

<valarray>

Defined in header

<vector>

template<classC>autorbegin(C&c)noexcept(noexcept(c.rbegin()))->decltype(c.rbegin()); (1)(since C++14)
(constexpr since C++17)template<classC>autorbegin(constC&c)noexcept(noexcept(c.rbegin()))->decltype(c.rbegin()); (2)(since C++14)
(constexpr since C++17)template<classT,std::size_tN>std::reverse_iterator<T*>rbegin(T(&array)[N])noexcept; (3)(since C++14)
(constexpr since C++17)template<classT>std::reverse_iterator<constT*>rbegin(std::initializer_list<T>il)noexcept; (4)(since C++14)
(constexpr since C++17)template<classC>autocrbegin(constC&c)noexcept(noexcept(std::rbegin(c)))->decltype(std::rbegin(c)); (5)(since C++14)
(constexpr since C++17)Returns an iterator to the reverse-beginning of the given range.

1,2) Returns c.rbegin(), which is typically an iterator to the reverse-beginning of the sequence represented by c.

1) If C is a standard

Container

, returns a C::reverse_iterator object.

2) If C is a standard

Container

, returns a C::const_reverse_iterator object.

3) Returns a std::reverse_iterator<T*> object to the reverse-beginning of array.

4) Returns a std::reverse_iterator<constT*> object to the reverse-beginning of il.

5) Returns std::rbegin(c), with c always treated as const-qualified.

If C is a standard

Container

, returns a C::const_reverse_iterator object.

Parameters

c - a container or view with a rbegin member function array - an array of arbitrary type il - an std::initializer_listReturn value

1,2)c.rbegin()

3)std::reverse_iterator<T*>(array+N)

4)std::reverse_iterator<constT*>(il.end())

5)std::rbegin(c)

Exceptions

1,2) What and when underlying c.rbegin() call throws.

5) What and when underlying std::rbegin(c) call throws.

Overloads

Custom overloads of rbegin may be provided for classes and enumerations that do not expose a suitable rbegin() member function, yet can be iterated.

Notes

The overload for

std::initializer_list

is necessary because it does not have a member function rbegin.

Example

Run this code

#include<iostream>#include<iterator>#include<vector>intmain(){std::vector<int>v={3,1,4};autovi=std::rbegin(v);// the type of “vi” is std::vector<int>::reverse_iteratorstd::cout<<"*vi = "<<*vi<<'\n';*std::rbegin(v)=42;// OK: after assignment v[2] == 42// *std::crbegin(v) = 13; // error: the location is read-onlyinta[]={-5,10,15};autoai=std::rbegin(a);// the type of “ai” is std::reverse_iterator<int*>std::cout<<"*ai = "<<*ai<<'\n';autoil={3,1,4};// the type of “it” below is std::reverse_iterator<int const*>:for(autoit=std::rbegin(il);it!=std::rend(il);++it)std::cout<<*it<<' ';std::cout<<'\n';}Output:

*vi = 4 *ai = 15 4 1 3 Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior

LWG 3537

C++14 std::rbegin and std::crbegin were not required to propagate exception specification required See also

(C++11)(C++14)

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

[edit]

(C++11)(C++14)

returns an iterator to the end of a container or array
(function template)

[edit]

(C++14)

returns a reverse end iterator for a container or array
(function template)

[edit]

(C++20)

returns a reverse iterator to a range
(customization point object)

[edit]

(C++20)

returns a reverse iterator to a read-only range
(customization point object)

[edit]