Reputation: 2158
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
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