C++ Standard Library C++ STL Library

C++ <iterator> - rend() Function



The C++ <iterator> rend() function returns an iterator pointing to the reverse past-the-last 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 past-the-last element of the array.
  • initializer_list: The function returns reverse_iterator<const T*> to the reverse past-the-last element 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 rend (Container& cont) 
    -> decltype (cont.rend());
template <class Container>
  auto rend (const Container& cont) 
    -> decltype (cont.rend());

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

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

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

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

Parameters

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

Return Value

Returns an iterator to the reverse past-the-last 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 past-the-last element of the array.
  • initializer_list: The function returns reverse_iterator<const T*> to the reverse past-the-last element of the <initializer_list>.

Example:

In the example below, the rend() 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 rend() 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