C++ Standard Library C++ STL Library

C++ <iterator> - rbegin() Function



The C++ <iterator> rbegin() function returns an iterator pointing to the reverse-beginning element in the sequence.

  • Container: If the container's object is constant qualified, the function returns a const_reverse_iterator, else returns an reverse_iterator.
  • Array: The function returns reverse_iterator<T*> to the reverse-beginning of the array.
  • initializer_list: The function returns reverse_iterator<const T*> to the reverse-beginning of the <initializer_list>.
C++ rbegin rend

These function templates are defined in multiple headers which are: <iterator>, <array>, <deque>, <forward_list>, <list>, <map>, <regex>, <set>, <string>, <string_view>, <unordered_map>, <unordered_set> and <vector>.

Syntax

//container version
template <class Container>
  auto rbegin (Container& cont) 
    -> decltype (cont.rbegin());
template <class Container>
  auto rbegin (const Container& cont) 
    -> decltype (cont.rbegin());

//array version
template <class T, size_t N>
  reverse_iterator<T*> rbegin (T(&arr)[N]);

//initializer_list version
template <class T>
  reverse_iterator<const T*> 
    rbegin (initializer_list<T> il);
//container version
template <class Container>
  constexpr auto rbegin (Container& cont) 
    -> decltype (cont.rbegin());
template <class Container>
  constexpr auto rbegin (const Container& cont) 
    -> decltype (cont.rbegin());

//array version
template <class T, size_t N>
  constexpr reverse_iterator<T*> rbegin (T(&arr)[N]);

//initializer_list version
template <class T>
  reverse_iterator<const T*> 
    constexpr rbegin (initializer_list<T> il);

Parameters

cont Specify a container or view with a rbegin member function.
arr Specify an array.
il Specify an initializer_list.

Return Value

Returns an iterator to the reverse-beginning element of the sequence:

  • Container: If the container's object is constant qualified, the function returns a const_reverse_iterator, else returns an reverse_iterator.
  • Array: The function returns reverse_iterator<T*> to the reverse-beginning of the array.
  • initializer_list: The function returns reverse_iterator<const T*> to the reverse-beginning of the <initializer_list>.

Example:

In the example below, the rbegin() function is used to iterate over an array to insert all elements of the array into a vector in reverse order.

#include <iostream>
#include <iterator>
#include <vector>
using namespace std;
 
int main (){
  int arr[] = {10, 20, 30, 40, 50};
  vector<int> vec;

  //iterate over array to insert all elements of the
  //array into an empty vector in reverse order
  for(auto it = rbegin(arr); it != rend(arr); ++it)
    vec.push_back(*it);

  //print the content of the vector
  cout<<"vec contains: ";
  for(auto it = begin(vec); it != end(vec); ++it)
    cout<<*it<<" ";

  return 0;
}

The output of the above code will be:

vec contains: 50 40 30 20 10

Example:

Similarly, the rbegin() function can be used with initializer_list.

#include <iostream>
#include <iterator>
#include <vector>
using namespace std;
 
int main (){
  initializer_list<int> ilist = {10, 20, 30, 40, 50};
  vector<int> vec;

  //iterate over initializer_list to insert all 
  //elements of the initializer_list into an 
  //empty vector in reverse order
  for(auto it = rbegin(ilist); it != rend(ilist); ++it)
    vec.push_back(*it);

  //print the content of the vector
  cout<<"vec contains: ";
  for(auto it = begin(vec); it != end(vec); ++it)
    cout<<*it<<" ";

  return 0;
}

The output of the above code will be:

vec contains: 50 40 30 20 10

❮ C++ <iterator> Library

5