maregor
maregor

Reputation: 797

Adding into unordered map containing vector value

Let's say I have an unordered map defined as this:

unordered_map<string, vector<Obj*>> map // I defined some Obj class

If I were to add an element into the map, I would have to do something like:

pair<string, vector<Obj*>> newEntry(myString, myVector);
map.insert(newEntry);

But I just want to add an element into the vector, what can I do? Do I have to pull out the vector and add to it before inserting it again? What is the best way to do this?

Upvotes: 0

Views: 1607

Answers (1)

P0W
P0W

Reputation: 47784

You can use std::unordered_map::operator[] as following :

map[myString].push_back( new_object ); // new_obj is of type Obj*

This will update myString key's value, or insert the new key-value if not present.

Also avoid using map as variable name, which conflicts with std::map

Upvotes: 1

Related Questions