Namespaces
Variants
Views
Actions

std::rbegin, std::crbegin

From cppreference.com
< cpp‎ | iterator
 
 
Iterator library
Iterator concepts
Iterator primitives
Algorithm concepts and utilities
Indirect callable concepts
Common algorithm requirements
Utilities
Iterator adaptors
Stream iterators
Iterator customization points
Iterator operations
(C++11)
(C++11)
Range access
(C++11)(C++14)
(C++11)(C++14)
(C++17)(C++20)
rbegincrbegin
(C++14)(C++14)
(C++14)(C++14)
(C++17)
(C++17)
 
Defined in header <array>
Defined in header <deque>
Defined in header <forward_list>
Defined in header <iterator>
Defined in header <list>
Defined in header <map>
Defined in header <regex>
Defined in header <set>
Defined in header <span>
(since C++20)
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 <vector>
(1)
template< class C >
auto rbegin( C& c ) -> decltype(c.rbegin());
(since C++14)
(until C++17)
template< class C >
constexpr auto rbegin( C& c ) -> decltype(c.rbegin());
(since C++17)
(1)
template< class C >
auto rbegin( const C& c ) -> decltype(c.rbegin());
(since C++14)
(until C++17)
template< class C >
constexpr auto rbegin( const C& c ) -> decltype(c.rbegin());
(since C++17)
(2)
template< class T, size_t N >
reverse_iterator<T*> rbegin( T (&array)[N] );
(since C++14)
(until C++17)
template< class T, size_t N >
constexpr reverse_iterator<T*> rbegin( T (&array)[N] );
(since C++17)
(3)
template< class C >
auto crbegin( const C& c ) -> decltype(std::rbegin(c));
(since C++14)
(until C++17)
template< class C >
constexpr auto crbegin( const C& c ) -> decltype(std::rbegin(c));
(since C++17)

Returns an iterator to the reverse-beginning of the given container c or array array.

1) Returns a possibly const-qualified iterator to the reverse-beginning of the container c.
2) Returns std::reverse_iterator<T*> to the reverse-beginning of the array array.
3) Returns a const-qualified iterator to the reverse-beginning of the container c.

range-rbegin-rend.svg

Contents

[edit] Parameters

c - a container with a rbegin method
array - an array of arbitrary type

[edit] Return value

An iterator to the reverse-beginning of c or array

[edit] 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:

specializes std::rbegin
(function) [edit]

[edit] Example

#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:

4
15

[edit] 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]