I have an std:map defined as follows:
std:map<std::string country, std::vector<SomeClass> events>;
Holds some events that will happen in some countries in the world.
I want my main function to create this map given a name to the key but without inserting a value.
Let’s say that the key is the name of a country. I want to create the “position” “USA” without inserting an event that will happen in the future.
My event() function will enter a new even without checking if the key is valid
Is this possible? Somehow to enter a value_pair with “empty” value?
UPDATE: No boost …
Will inserting an empty
vectordo the trick?…You could then add actual events without checking of existence of map’s value:
I’m not sure whether this is what you’re looking for, since the notion of ‘creating “position” “USA”‘ looks ambiguous. (And earlier answers seem to pick different interpretation of it).
EDIT: As commenters pointed out, inserting the empty vector is not even necessary, since map values are automatically created (using default initializer) & inserted when one uses the
[]operator. Hence you you can just doevents["USA"].push_back(data)directly.