
- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- The C++ Advanced Library
- C++ Library - <any>
- C++ Library - <barrier>
- C++ Library - <bit>
- C++ Library - <chrono>
- C++ Library - <cinttypes>
- C++ Library - <clocale>
- C++ Library - <condition_variable>
- C++ Library - <coroutine>
- C++ Library - <cstdlib>
- C++ Library - <cstring>
- C++ Library - <cuchar>
- C++ Library - <charconv>
- C++ Library - <cfenv>
- C++ Library - <cmath>
- C++ Library - <ccomplex>
- C++ Library - <expected>
- C++ Library - <format>
- C++ Library - <future>
- C++ Library - <flat_set>
- C++ Library - <flat_map>
- C++ Library - <filesystem>
- C++ Library - <generator>
- C++ Library - <initializer_list>
- C++ Library - <latch>
- C++ Library - <memory_resource>
- C++ Library - <mutex>
- C++ Library - <mdspan>
- C++ Library - <optional>
- C++ Library - <print>
- C++ Library - <ratio>
- C++ Library - <scoped_allocator>
- C++ Library - <semaphore>
- C++ Library - <source_location>
- C++ Library - <span>
- C++ Library - <spanstream>
- C++ Library - <stacktrace>
- C++ Library - <stop_token>
- C++ Library - <syncstream>
- C++ Library - <system_error>
- C++ Library - <string_view>
- C++ Library - <stdatomic>
- C++ Library - <variant>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ vector::rend() Function
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.
Syntax
Following 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 1
Let'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 11
Example 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