Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I determine if a particular key is unique in a multimap?

Tags:

c++

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.

like image 834
ReinstateMonica Larry Osterman Avatar asked Jul 12 '12 06:07

ReinstateMonica Larry Osterman


People also ask

How do I get my multimap key?

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.

How do I get rid of multimap 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.

How is multimap sorted?

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.


1 Answers

Could you check if the std::multimap::count(key) == 1?

like image 183
Prashant Kumar Avatar answered Sep 22 '22 15:09

Prashant Kumar