I\'m having a std::vector
with elements of some class ClassA
. Additionally I want to create an index using a std::map
Just make them both store pointers an explicitly delete the objects when you don't need them.
std::vector storage;
std::map map;
for (int i=0; i<10000; ++i) {
ClassA* a = new ClassA()
storage.push_back(a)
map.insert(std::make_pair(a->getKey(), a))
}
// map contains only valid pointers to the 'correct' elements of storage