stdrbeginstdcrbegin(3)
std::rbegin,std::crbegin - std::rbegin,std::crbegin
Synopsis
Defined in header<iterator>
template <class C>
(since C++ 14)
auto rbegin(C &c) -> decltype(c.rbegin());
(until C++ 17)
template <class C>
(since C++ 17)
constexpr auto rbegin(C &c) -> decltype(c.rbegin());
template <class C>
(since C++ 14)
auto rbegin(const C &c) -> decltype(c.rbegin());
(until C++ 17)
template <class C>
(since C++ 17)
constexpr auto rbegin(const C &c) -> decltype(c.rbegin());
(1)
template <class T, size_t N>
(since C++ 14)
reverse_iterator<T *> rbegin(T (&array)[N]);
(1)(until C++ 17)
template <class T, size_t N>
(since C++ 17)
constexpr reverse_iterator<T *> rbegin(T (&array)[N]);
(2)
template <class C>
(since C++ 14)
auto crbegin(const C &c) -> decltype(std::rbegin(c));
(3)(until C++ 17)
template <class C>
(since C++ 17)
constexpr auto crbegin(const C &c) -> decltype(std::rbegin(c));Returns an iterator to the reverse-beginning of the given container c or array array.
- Returns a possibly const-qualified iterator to the reverse-beginning of the container c.
- Returns std::reverse_iterator<T*> to the reverse-beginning of the array array.
- Returns a const-qualified iterator to the reverse-beginning of the container c. range-rbegin-rend.svg
Parameters
c - a container with a rbegin method array - an array of arbitrary type
Return value
An iterator to the reverse-beginning of c or array
Notes
In addition to being included in <iterator>, std::rbegin and std::crbegin are guaranteed to become available if any of the following headers are included: <array>, <deque>, <forward_list>, <list>, <map>, <regex>, <set>
, <span>
(since C++20), <string>
, <string_view>
(since C++17), <unordered_map>, <unordered_set>, and <vector>.
Overloads
Custom overloads of rbegin may be provided for classes that do not expose a suitable rbegin() member function, yet can be iterated. The following overload is already provided by the standard library:
rbegin(std::initializer_list) specializes std::rbegin
Example
// Run this code
#include <iostream>
#include <vector>
#include <iterator>
int main()
{
std::vector<int> v = {3, 1, 4};
auto vi = std::rbegin(v);
std::cout << *vi << ‘\n’;
int a[] = {-5, 10, 15};
auto ai = std::rbegin(a);
std::cout << *ai << ‘\n’;
}
Output:
See also
begin
cbegin returns an iterator to the beginning of a container or array