The C++ std::deque::rend() function is used to return the reverse iterator pointing to the element preceding the first element of the deque. This iterator is used in reverse iteration over the deque, typically in combination with rbegin(), which points to the last element. By iterating from rbegin() to rend(), you can access all the elements in reverse sequence.
SyntaxFollowing is the syntax for std::deque::rend() function.
reverse_iterator rend() noexcept; const_reverse_iterator rend() const noexcept;Parameters
It does not accept any parameter.
Return valueIt returns a reverse iterator to the reverse end of the sequence container.
ExceptionsThis function never throws exception.
Time complexityThe time complexity of this function is Constant i.e. O(1)
ExampleIn the following example, we are going to consider the basic usage of rend() function.
#include <iostream> #include <deque> int main() { std::deque<char> a = {'A', 'B', 'C', 'D'}; for (auto x = a.rbegin(); x != a.rend(); ++x) { std::cout << *x << " "; } return 0; }Output
Output of the above code is as follows −
D C B AExample
Consider the following example, where we are going to modify the deque in the reverse order.
#include <iostream> #include <deque> int main() { std::deque<int> a = {01,12,23,34}; for (auto x = a.rbegin(); x != a.rend(); ++x) { *x *= 3; } std::cout << "Modified deque: "; for (const auto& elem : a) { std::cout << elem << " "; } std::cout << std::endl; return 0; }Output
Following is the output of the above code −
Modified deque: 3 36 69 102
deque.htm
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