You seem to be confused with the basic concept of object instances. When you add something to a vector, you do not move it into it, you copy it:
vector<string> vec; string s; vec.push_back(s);
vec[0] not s , it is a copy of s . Therefore, when you remove it from a vector, s not affected.
If you do not want to copy, you should switch to pointers. You can remove pointers from a vector, and the destructor of the object they point to will not be called.
Edit: Well, it looks like you are already using pointers. You said:
reading documents for vector and other containers in C ++ shows that when objects popped up, their destructors are called
It's right. When you remove a pointer from a vector, the pointer becomes destroyed. This is what documents mean. This does not mean that the object pointed to by the pointer is destroyed:
vector<string*> vec; string s; vec.push_back(&s); vec.pop_back();
s is not affected at all. What is destroyed by the pop operation is a pointer that contains the address s , not s .
So, you are fine.
source share