I have a vector containing objects of type STL map, and I do vector.push_back(some map).
This unfortunately calls the map copy constructor, and wastes a lot of time. I understand that i can get around this by keeping a vector of (smart) pointers to maps – but this got me wondering – I read that STL anyway keeps its data on the heap and not on the stack – so why is the copy ctor not O(1) time, by simply copying pointers?
If you don’t need the original map anymore after pushing back a copy back into the vector, write:
If you don’t have a C++11 compiler yet, add an empty map and then swap that with the original: