I have a multimap<key_type,value_type>
and I would like to know if a particular key appears in the map at most one time.
I know I can call multimap.equal_range(key)
to find an iterator to the start and end of the range containing key
but I would like to know if there is only one element between the range.first
and range.second
.
Is there a better way than incrementing the range.first
value to see if it is equal to range.end
? Since multimap::iterator
is bidirectional it's not a huge deal to undo the increment but it seems sloppy to do that.
We can find all values of a key in Multimap using is member function equal_range(). It accepts the key as an argument and returns a pair of multimap iterator. This returned pair has a range that represents the entries with given key.
erase() is used to remove or erase elements from a multimap container. This function can remove or erase the elements by its key, position or the given range. When we run this function the size of the multimap container is reduced by the number of elements being removed.
Multimap is an associative container that contains a sorted list of key-value pairs, while permitting multiple entries with the same key. Sorting is done according to the comparison function Compare , applied to the keys. Search, insertion, and removal operations have logarithmic complexity.
Could you check if the std::multimap::count(key) == 1?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With