C++ .rbegin()
In C++, the .rbegin() function is used to obtain a reverse iterator pointing to the last element of a container, such as a vector, map, or other standard containers. This allows iteration from the end toward the beginning of the container. The .rbegin() function is typically used in conjunction with the .rend() function, which returns a reverse iterator pointing just before the first element of the container (i.e., the reverse end).
Syntax
container.rbegin();
Parameters:
The .rbegin() function takes no parameters.
Return value:
The function returns a reverse iterator that points to the last element of the container. If the container contains no elements, the returned iterator will be equal to .rend(), indicating that there are no elements to iterate over in reverse.
Example
The following exmaple demonstrates the usage of the .rbegin() function:
#include <iostream>#include <map>int main() {std::map<std::string, int> myMap = {{"one", 1}, {"two", 2}, {"three", 3}};std::cout << "Elements in reverse order:\n";for (auto it = myMap.rbegin(); it != myMap.rend(); ++it) {std::cout << it->first << ": " << it->second << "\n";}return 0;}
The output of this code will be:
Elements in reverse order:three: 3two: 2one: 1
The .rbegin() function is used to obtain a reverse iterator that refers to the last element of the map. The loop iterates over the elements in reverse order, printing each key-value pair. The output shows the elements in reverse order compared to their original insertion order.
Codebyte Example
Run the following code to understand how the .rbegin() function works:
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
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.
- Beginner Friendly.11 hours