-1

i would like to know if is safe to do this:

std::vector<int*> v;
...
//no deleting
v.erase(v.begin());

With safe i mean that by doing this, we are not creating garbage, and so that std::vector<T>::erase is calling himself delete pointer;, or if instead i should have done manually the deleting:

std::vector<int*> v;
...
delete v[0];
v.erase(v.begin());
Alberto Sinigaglia
  • 12,097
  • 2
  • 20
  • 48

1 Answers1

1

Standard containers, like std::vector, containing raw pointers DO NOT automatically delete the things that the pointers are pointing at, when removing the pointers from the containers. If you want that, store smart pointers instead, ie std::unique_ptr or std::shared_ptr.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770