The C++ vector::rend() function returns a reverse iterator pointing to the vector's element that is preceding the first element (reversed past-the-last element). When increased, a reverse iterator moves to the beginning of the vector container and iterates in the opposite manner.
Similar to increasing an iterator, reducing a reverse iterator causes the vector container to move to the end. The function returns a const_reverse_iterator if the vector object is const-qualified. If not, a reverse iterator is returned. The time complexity of the rend() function is constant.
SyntaxFollowing is the syntax for C++ vector::rend() Function −
reverse_iterator rend() noexcept; const_reverse_iterator rend() const noexcept;Parameters
It doesn't accept any kind of parameter.
Example 1Let's consider the following example, where we are going to use reserve() function.
#include <iostream> #include<vector> using namespace std; int main(){ vector<int> tutorial{11,22,33,44}; vector<int>::reverse_iterator x; for(x=tutorial.rbegin();x!=tutorial.rend();x++) std::cout<< *x<<" "; return 0; }Output
When we compile and run the above program, this will produce the following result −
44 33 22 11Example 2
Considering the another scenario, where we are going to take string type and applying the rend() function.
#include <iostream> #include<vector> using namespace std; int main() { vector<string> course{"HTML","SQL","JS","OS"}; vector<string>::reverse_iterator x; vector<string>::iterator a; std::cout<<"before using rend():"; for(a=course.begin();a!=course.end();a++) cout<<*a<<", "; cout<<'\n'; cout<<"after using rend():"; for(x=course.rbegin();x!=course.rend();x++) cout<<*x<<", "; return 0; }Output
On running the above program, it will produce the following result −
before using rend():HTML, SQL, JS, OS, after using rend():OS, JS, SQL, HTML,Example 3
In the following example, we are going to use the push_back() function to insert the elements and applying the rend() function.
#include <iostream> #include<vector> using namespace std; int main(){ vector<string> car; car.push_back("RS7"); car.push_back("Q6"); car.push_back("GLS340D"); car.push_back("CHERON"); cout << "\nResult:\n"; for (auto x = car.rbegin(); x != car.rend(); x++) cout << *x << " "; return 0; }Output
When we execute the above program, it will produce the following result −
Result: CHERON GLS340D Q6 RS7
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4