Question : Storing large objects in a map

I have a large object that I'm storing many of in a map. My question is should I be storing references of the object in the map instead of the map object itself? Is it by default being copied into the map?

std::map<int, MyLargeObj> myMap;

When I run myMap.insert(key, large_obj) is it copying the entire object into the map or is creating a reference to it somehow? Should I instead be defining it like std::map<int, MyLargeObj&>?


Answer : Storing large objects in a map

If the object are so big, I'd create these objects in the heap (with new operator) and store the pointers in the map. I'd make a class that will manage this store and will have a member of this map. This class may have a Clear() method that will delete all MyLargeObj's. This Clear should be called from the class destructor. This way will allow to hide the implementation of the MyLargeObj even, if it is needed.

Random Solutions  
programming4us programming4us