The Wayback Machine - https://web.archive.org/web/20240527123729/https://www.geeksforgeeks.org/vector-rbegin-and-rend-function-in-c-stl/
Open In App

vector rbegin() and rend() function in C++ STL

Last Updated : 31 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

vector::rbegin() is a built-in function in C++ STL which returns a reverse iterator pointing to the last element in the container. 
Syntax: 

vector_name.rbegin()

Parameters: The function does not accept any parameter.
Return value: The function returns a reverse iterator pointing to the last element in the container.
Program to demonstrate the vector::rbegin() method: 
Program 1: 

CPP




// CPP program to illustrate
// the vector::rbegin() function
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    vector<int> v;
    v.push_back(11);
    v.push_back(12);
    v.push_back(13);
    v.push_back(14);
    v.push_back(15);
 
    // prints all the elements
    cout << "The vector elements in reverse order are:\n";
    for (auto it = v.rbegin(); it != v.rend(); it++)
        cout << *it << " ";
    return 0;
}


Output: 

The vector elements in reverse order are:
15 14 13 12 11

 

Time Complexity – constant O(1)

vector::rend() is a built-in function in C++ STL which returns a reverse iterator pointing to the theoretical element right before the first element in the vector container.
Syntax: 

vector_name.rend()

Parameters: The function does not take any parameter.
Return value: The function returns a reverse iterator pointing to the theoretical element right before the first element in the vector container.
Program to demonstrate the vector::rend() method: 
Program 1: 

CPP




// CPP program to illustrate
// the vector::rend() function
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    vector<int> v;
    v.push_back(11);
    v.push_back(12);
    v.push_back(13);
    v.push_back(14);
    v.push_back(15);
 
    cout << "The last element is: " << *v.rbegin();
 
    // prints all the elements
    cout << "\nThe vector elements in reverse order are:\n";
    for (auto it = v.rbegin(); it != v.rend(); it++)
        cout << *it << " ";
    return 0;
}


Output: 

The last element is: 15
The vector elements in reverse order are:
15 14 13 12 11

 

 Time Complexity – constant O(1)



Previous Article
Next Article

Similar Reads

list rbegin() and rend() function in C++ STL
list::rbegin() is an inbuilt function in C++ STL that returns a reverse iterator which points to the last element of the list. Syntax: list_name.rbegin()Parameter: This function does not accept any parameters. Return value: It returns a reverse iterator which points to the last element of the list. Below program illustrates the above function: C/C+
2 min read
multiset rbegin() and rend() function in C++ STL
multiset::rbegin() is a built-in function in C++ STL which returns a reverse iterator pointing to the last element in the multiset container. Syntax: reverse_iterator multiset_name.rbegin() Parameters: The function does not take any parameter. Return value: The function returns a reverse iterator pointing to the last element in the container. Below
2 min read
set::rbegin() and set::rend() in C++ STL
set::rbegin() is a built-in function in C++ STL which returns a reverse iterator pointing to the last element in the container. Syntax: reverse_iterator set_name.rbegin() Parameters: The function does not take any parameter. Return value: The function returns a reverse iterator pointing to the last element in the container. Program to demonstrate t
2 min read
array::rbegin() and array::rend() in C++ STL
array::rbegin() is a built-in function in C++ STL which returns a reverse iterator pointing to the last element in the container. Syntax:array_name.rbegin()Parameters: The function does not accept any parameter. Return value: The function returns a reverse iterator pointing to the last element in the container. Program to demonstrate the array::rbe
2 min read
deque rend() function in C++ STL
The deque::rend() is an inbuilt function in C++ STL which returns a reverse iterator which points to the position before the beginning of the deque (which is considered its reverse end). Syntax: deque_name.rend()Parameter: This function does not accept any parameters. Return value: It returns a reverse iterator which points to the position before t
2 min read
map rend() function in C++ STL
The rend() function is an inbuilt function in C++ STL which returns a reverse iterator pointing to the theoretical element right before the first key-value pair in the map(which is considered its reverse end). Syntax: map_name.rend() Parameters:The function does not take any parameter. Return Value: The function returns a reverse iterator pointing
2 min read
deque rbegin() function in C++ STL
The deque::rbegin() is an inbuilt function in C++ STL which returns a reverse iterator which points to the last element of the deque (i.e., its reverse beginning). Syntax: deque_name.rbegin()Parameter: This function does not accept any parameters. Return value: It returns a reverse iterator which points to the last element of the deque. Below progr
2 min read
map rbegin() function in C++ STL
The std::map::rbegin() is a function in C++ STL. It returns a reverse iterator which points to the last element of the map. The reverse iterator iterates in reverse order and incrementing it means moving towards beginning of map. Syntax: r_i rbegin(); const_r_i rbegin() const; Parameters: It does not except any parameters. Returns: It returns a rev
2 min read
multimap rend in C++ STL
multimap::rend() is a built-in function in C++ STL which returns a reverse iterator pointing to the theoretical element preceding to the first element of the multimap container. Syntax multimap_name.rend() Parameters: The function does not take any parameter. Return ValueThe function returns a reverse iterator pointing to the reverse end of the mul
2 min read
multimap rbegin in C++ STL
multimap::rbegin() is a built-in-function in C++ STL which returns an iterator pointing to the last element of the container. Syntax: multimap_name.rbegiin() Parameters: The function does not take any parameter. Return Value: The function returns a reverse iterator pointing to the last element of the container. (Note: Reverse iterators iterate back
2 min read
Article Tags :
Practice Tags :