How to "reserve" memory for maps

I have a situation where map elements will be inserted all throughout a continuous loop. I know that there's no reserve function for maps like there are for vectors, but I know exactly the maximum amount of elements there will be in the map, I just don't know the value of each individual map.

Would it be possible if I just inserted the maximum amount of elements in the beginning of the program, and change the value of each/any individual element later on? For example:

1
2
3
4
5
6
7
8
std::map<int, std::string> map;
//There will be a maximum of 200 elements in this map
for (int i = 0; i < 200; i++)
{
     map[i];
}

//Change the value of individual elements as I wish 


Is doing so a good idea? Is it more efficient than initializing on declaration if initializing will be done often?

Thanks in advane
1. There is no way to modify a key; the value_type of a map is std::pair<const Key, T>

2. std::string will need to allocate memory if it is changed from an empty string to a non-empty string.

3. Inserting 200 (or 2000) items doesn't take a significant amount of time; just don't bother.
Got it. Thanks for the help.
Topic archived. No new replies allowed.