Std::map
lastmod 2019-08-21created 2015-08-276 edits
Observers
-
key_comp - Return key comparison object.
-
value_comp - Return value comparison object.
Lookup methods
-
count - returns the number of elements matching specific key.
-
find - finds element with specific key.
-
equal_range - returns range of elements matching a specific key.
- Returns the range of a specific key value as a pair of iterators.
-
lower_bound - returns an iterator to the first element not less than the given value.
- If an element with the given key exists, returns an iterator to that position.
-
upper_bound - returns an iterator to the first element greater than a certain value.
- Returns the first iterator one unit greater than the given key.
Insert vs []
When inserting data into a std::map, the differences between insert() and array element access ([]) are as follows.
- insert: ignores the value if the key is a duplicate.
- [key]: changes the value if a value exists for the key. Adds it if the key has no value.
Example
Getting iterators to duplicate elements
An example using equal_range is as follows.
multimap<int, string> m;
// ...
pair< multimap<int, string>::iterator, multimap<int, string>::iterator > p = m.equal_range(2);
for(i=p.first; i!=p.second; i++) { // Key 값이 2인 모든 원소 조회
cout << i->second << endl;
}
Alternatively, you can use lower_bound and upper_bound.
for(i=m.lower_bound(2); i!=m.upper_bound(2); i++) { // Key 값이 2인 모든 원소 조회
cout << i->second << endl;
}
Deleting all entries for a specific Key
An example using equal_range is as follows. multimap
</syntaxhighlight>
Favorite site
Pages that link here