Searching in std::map or in std::multimap
suggest changeThere are several ways to search a key in std::map
or in std::multimap
.
- To get the iterator of the first occurrence of a key, the
find()
function can be used. It returnsend()
if the key does not exist.
std::multimap< int , int > mmp{ {1, 2}, {3, 4}, {6, 5}, {8, 9}, {3, 4}, {6, 7} };
auto it = mmp.find(6);
if (it!=mmp.end())
std::cout << it->first << ", " << it->second << std::endl; //prints: 6, 5
else
std::cout << "Value does not exist!" << std::endl;
it = mmp.find(66);
if (it!=mmp.end())
std::cout << it->first << ", " << it->second << std::endl;
else
std::cout << "Value does not exist!" << std::endl; // This line would be executed.
- Another way to find whether an entry exists in
std::map
or instd::multimap
is using thecount()
function, which counts how many values are associated with a given key. Sincestd::map
associates only one value with each key, itscount()
function can only return 0 (if the key is not present) or 1 (if it is). Forstd::multimap
,count()
can return values greater than 1 since there can be several values associated with the same key.
std::map< int , int > mp{ {1, 2}, {3, 4}, {6, 5}, {8, 9}, {3, 4}, {6, 7} };
if (mp.count(3) > 0) // 3 exists as a key in map
std::cout << "The key exists!" << std::endl; // This line would be executed.
else
std::cout << "The key does not exist!" << std::endl;
If you only care whether some element exists, find
is strictly better: it documents your intent and, for multimaps
, it can stop once the first matching element has been found.
- In the case of
std::multimap
, there could be several elements having the same key. To get this range, theequal_range()
function is used which returnsstd::pair
having iterator lower bound (inclusive) and upper bound (exclusive) respectively. If the key does not exist, both iterators would point toend()
.
auto eqr = mmp.equal_range(6);
auto st = eqr.first, en = eqr.second;
for (auto it = st; it != en; ++it){
std::cout << it->first << ", " << it->second << std::endl;
}
// prints: 6, 5
// 6, 7
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents
2 Literals
9 Arrays
10 Flow control
12 Loops
15 keywords
17 auto keyword
18 Pointers
21 std::string
22 Enumeration
24 std::vector
25 std::array
26 std::pair
27 std::map
30 std::any
31 std::variant
36 std::iomanip
37 Iterators
38 Basic I/O
39 File I/O
40 Streams
44 References
45 Polymorphism
52 Unions
53 Templates
54 Namespaces
57 Lambdas
58 Threading
60 Preprocessor
61 SFINAE
63 RAII
64 Exceptions
68 Sorting
75 Pimpl idiom
76 Copy elision
79 Singleton
82 Type erasure
85 RTTI
88 Scopes
89 Atomic types
91 constexpr
99 Type traits
103 Attributes
105 Profiling
108 Recursion
109 Callable objects
112 Inline functions
114 Header files
117 Parameter packs
118 Iteration
119 type deduction
121 Build systems
123 Type inference
126 Alignment
127 Inline variables
134 Optimization
135 Semaphore
137 Debugging
140 Mutexes
141 Recursive mutex
142 Unit testing
143 decltype
144 Digit separators
145 C++ Containers
147 Contributors