Skip to content

Commit ba74c56

Browse files
authored
[Term Entry] C++ Maps: .equal_range()
* Create equal range entry for cpp * Update equal-range.md * Update content/cpp/concepts/maps/terms/equal-range/equal-range.md Co-authored-by: Mamta Wardhani <mamta.wardhani@gmail.com> * Update content/cpp/concepts/maps/terms/equal-range/equal-range.md Co-authored-by: Mamta Wardhani <mamta.wardhani@gmail.com> * Update content/cpp/concepts/maps/terms/equal-range/equal-range.md Co-authored-by: Mamta Wardhani <mamta.wardhani@gmail.com> * Update content/cpp/concepts/maps/terms/equal-range/equal-range.md Co-authored-by: Mamta Wardhani <mamta.wardhani@gmail.com> * Update content/cpp/concepts/maps/terms/equal-range/equal-range.md Co-authored-by: Mamta Wardhani <mamta.wardhani@gmail.com> * Update equal-range.md * Update equal-range.md * minor fixes * fixed formatting * Minor changes ---------
1 parent 971d94d commit ba74c56

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
---
2+
Title: '.equal_range()'
3+
Description: 'Returns a pair of iterators defining the range of elements with the given key.'
4+
Subjects:
5+
- 'Code Foundations'
6+
- 'Computer Science'
7+
Tags:
8+
- 'Iterators'
9+
- 'Map'
10+
- 'STL'
11+
CatalogContent:
12+
- 'learn-c-plus-plus'
13+
- 'paths/computer-science'
14+
---
15+
16+
In C++, the **`.equal_range()`** function returns a pair of iterators that define the range of elements matching a given key in an associative container. It is especially useful with `multimap`, where multiple elements can have the same key, allowing developers to retrieve all of them efficiently. In a `map`, where keys are unique, the returned range will contain at most one element.
17+
18+
## Syntax
19+
20+
```pseudo
21+
mapName.equal_range(key);
22+
```
23+
24+
**Parameters:**
25+
26+
- `key`: The key value to search for in the map.
27+
28+
**Return value:**
29+
30+
- Returns a `std::pair` of iterators:
31+
32+
- The first iterator points to the first element equal to the key (or the position where it would be inserted).
33+
- The second iterator points to the element just past the last element equal to the key.
34+
35+
## Example
36+
37+
This code uses `.equal_range` to find and print the element(s) with key 3 in a `std::map`:
38+
39+
```cpp
40+
#include <iostream>
41+
#include <map>
42+
43+
int main() {
44+
std::map<int, std::string> myMap = {
45+
{1, "apple"},
46+
{2, "banana"},
47+
{3, "cherry"},
48+
{4, "date"},
49+
{5, "elderberry"}
50+
};
51+
52+
// Find the range of elements with key 3
53+
auto range = myMap.equal_range(3);
54+
55+
if (range.first != range.second) {
56+
std::cout << "Elements with key 3:" << std::endl;
57+
for (auto it = range.first; it != range.second; ++it) {
58+
std::cout << it->first << " => " << it->second << std::endl;
59+
}
60+
} else {
61+
std::cout << "No elements with key 3 found." << std::endl;
62+
}
63+
64+
return 0;
65+
}
66+
```
67+
68+
The output produced by this code is:
69+
70+
```shell
71+
Elements with key 3:
72+
3 => cherry
73+
```
74+
75+
The code creates a `std::map` of integers to strings and uses the `.equal_range()` function to find the range of elements with the key `3`. Since `std::map` stores unique keys, the resulting range will contain at most one element. If the key exists, the range will include that element; otherwise, the range will be empty.
76+
77+
## Codebyte Example
78+
79+
Run this codebyte to understand how the `.equal_range()` function works:
80+
81+
```codebyte/cpp
82+
#include <iostream>
83+
#include <map>
84+
85+
int main() {
86+
std::map<int, std::string> fruits = {
87+
{1, "apple"},
88+
{2, "banana"},
89+
{3, "cherry"},
90+
{4, "date"}
91+
};
92+
93+
int key = 2;
94+
auto range = fruits.equal_range(key);
95+
96+
if (range.first != range.second) {
97+
std::cout << "Element with key " << key << ": ";
98+
for (auto it = range.first; it != range.second; ++it) {
99+
std::cout << it->first << " => " << it->second << std::endl;
100+
}
101+
} else {
102+
std::cout << "No element with key " << key << " found." << std::endl;
103+
}
104+
105+
return 0;
106+
}
107+
```

0 commit comments

Comments
 (0)