Is the destructor called when an item is removed from the STL container?

Let's say I have two containers that hold pointers to the same objects:

std::list<Foo*> fooList;
std::vector<Foo*> fooVec;

Let's say I delete an object from one of these containers through one if its methods are:

std::vector<Foo*>::iterator itr = 
  std::find( fooVec.begin(), fooVec.end(), pToObj );
fooVec.erase( itr );

CppReference says this calls the object's destructor. Does this mean that the pointer to the object in fooListis a sagging pointer?

I would prefer not to use reference counted pointers. How to deal with this problem?

+3
source share
5 answers

No.

, , , - , . (.. .)

. :

std::vector<int*> v;
v.push_back(new int());
v.push_back(new int());
v.push_back(new int());

, . , . , , , . ( , .)

, , , shared_ptr; , .

+6

, . fooList .

, , erase , . delete .

+1

, , .

( ?), , , - ( ), . , deque -. , deque , , deque .

0

(Foo *) - , - (, , ), Foo. ( Foo) , , .

0

, , , . Foo*, , 3 (, fooList fooVec), . , erase, , , , - op ( , GMan).

0
source

Source: https://habr.com/ru/post/1756348/


All Articles