Question

I don't see a way to map multiple values to one key in a boost::unordered_map. My map is declared as:

boost::unordered_map< uint16_t, Myclass* > myMap_;

I think I could achieve this by storing objects of MyClass in containers, i.e:

boost::unordered_map< uint16_t, vector<Myclass*> > myMap_;

however unordered_map provides the meanings to manage load factor size, therefore I wonder if it really can't or maybe it can somehow tie multiple values into a single key?

float max_load_factor() const;

Returns: Returns the current maximum load factor.

void max_load_factor(float z);

Effects: Changes the container's maximum load factor, using z as a hint.

size_type bucket_size(size_type n) const;

Requires: n < bucket_count() Returns: The number of elements in bucket n.


I know I can use multimap

typedef boost::unordered_multimap< int, MyClass*, MyHash<int> > HashMMap;

however I wondered if multiple values for a single key are possible somehow with a use of unordered_map.

Was it helpful?

Solution

From C++11 §23.2.5 Unordered associative containers

An unordered associative container supports unique keys if it may contain at most one element for each key. Otherwise, it supports equivalent keys. unordered_set and unordered_map support unique keys. unordered_multiset and unordered_multimap support equivalent keys.

If you want more than one value, you'll need to use a multimap (C++11 or from boost), or store a container as the value. A plain unordered_map will not do it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top