.rend()
Anonymous contributor
Published Nov 30, 2024
Contribute to Docs
The .rend()
function in C++ is part of the std::deque
container. It returns a reverse iterator pointing to the position before the first element of the container, allowing reverse iteration from the last element toward the first.
Syntax
deque_name.rend()
deque_name
: This represents the specificstd::deque
container that holds the data and is used to call the.rend()
function.
Example
The following code demonstrates how to iterate through a deque in reverse order using .rend()
:
#include <iostream>#include <deque>int main() {// Initialize a deque with integer valuesstd::deque<int> numbers = {1, 2, 3, 4, 5};// Output the deque in reverse order using reverse iteratorsstd::cout << "Deque elements in reverse: ";for (auto it = numbers.rbegin(); it != numbers.rend(); ++it) {std::cout << *it << " ";}return 0;}
This example results in the following output:
Deque elements in reverse: 5 4 3 2 1
In this example:
- The
.rbegin()
method starts the iteration at the last element of the deque. - The
.rend()
method determines where the reverse traversal stops, just before the first element.
Codebyte Example
Explore how .rend()
works with a character deque in this example:
In this example:
.rbegin()
initializes the reverse iteration from the last element..rend()
marks the stopping point of reverse traversal.
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn C++ on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn C++
Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.Beginner Friendly11 hours