Skip to content

Commit 7eef3a7

Browse files
authored
[Term Entry] C++ Maps: .rbegin()
* Create equal range entry for cpp * Create rbegin for cpp * Update rbegin.md * minor content fix * Delete content/cpp/concepts/maps/terms/equal-range/equal-range.md * fixed formatting * Minor changes ---------
1 parent ed47d09 commit 7eef3a7

File tree

1 file changed

+77
-0
lines changed
  • content/cpp/concepts/maps/terms/rbegin

1 file changed

+77
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
---
2+
Title: '.rbegin()'
3+
Description: 'Returns a reverse iterator pointing to the last element of the map.'
4+
Subjects:
5+
- 'Code Foundations'
6+
- 'Computer Science'
7+
Tags:
8+
- 'Functions'
9+
- 'Iterators'
10+
- 'Maps'
11+
- 'STL'
12+
CatalogContent:
13+
- 'learn-c-plus-plus'
14+
- 'paths/computer-science'
15+
---
16+
17+
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).
18+
19+
## Syntax
20+
21+
```pseudo
22+
container.rbegin();
23+
```
24+
25+
**Parameters:**
26+
27+
The `.rbegin()` function takes no parameters.
28+
29+
**Return value:**
30+
31+
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.
32+
33+
## Example
34+
35+
The following exmaple demonstrates the usage of the `.rbegin()` function:
36+
37+
```cpp
38+
#include <iostream>
39+
#include <map>
40+
41+
int main() {
42+
std::map<std::string, int> myMap = {{"one", 1}, {"two", 2}, {"three", 3}};
43+
std::cout << "Elements in reverse order:\n";
44+
for (auto it = myMap.rbegin(); it != myMap.rend(); ++it) {
45+
std::cout << it->first << ": " << it->second << "\n";
46+
}
47+
return 0;
48+
}
49+
```
50+
51+
The output of this code will be:
52+
53+
```shell
54+
Elements in reverse order:
55+
three: 3
56+
two: 2
57+
one: 1
58+
```
59+
60+
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.
61+
62+
## Codebyte Example
63+
64+
Run the following code to understand how the `.rbegin()` function works:
65+
66+
```codebyte/cpp
67+
#include <iostream>
68+
#include <map>
69+
70+
int main() {
71+
std::map<std::string, int> myMap = {{"one", 1}, {"two", 2}, {"three", 3}};
72+
for (auto it = myMap.rbegin(); it != myMap.rend(); ++it) {
73+
std::cout << it->first << ": " << it->second << "\n";
74+
}
75+
return 0;
76+
}
77+
```

0 commit comments

Comments
 (0)