I have a class X that I would like to put into an STL map of type std::map. An STL map needs to have X stored in memory somewhere so I'm looking for an efficient (run time and memory) way to create X and store it in the map.
I noticed that the following code where x is an object of type X and stlMap is a map of type std::map:
stlMap["test"] = x;
Results in the following being called:
- X default constructor
- X Copy constructor
- X Copy constructor
- X destructor
- X destructor
- X assignment constructor
- X destructor
Why are so many X objects being created?
Is it an inefficient use of time and memory?
Is there a better way to put an object into a map? Maybe changing the map to be a map of strings to x*?