PlsWork
PlsWork

Reputation: 2158

C++ working with a map with vectors as values

I have a map that maps strings to vectors of strings:

std::unordered_map<std::string, std::vector<std::string>>> myMap;.

Is there a nice way (as little code as possible while still being readable) to append a value to the vector of a given key?

How to handle the case of adding a value to a vector for a new key for which the vector hasn't been initialized yet?

Upvotes: 1

Views: 76

Answers (1)

HolyBlackCat
HolyBlackCat

Reputation: 96042

You want:

myMap["key"].push_back("string");

If the vector for this key doesn't exist, it will be created automatically.

Upvotes: 8

Related Questions