vector:: The prototype of erase is as follows:
iterator erase(iterator position);
iterator erase(iterator first, iterator last);
The corresponding instructions are as follows:
“
…
This effectively reduces the vector size by the number of elements removed, calling each element’s destructor before.
…
“
In the description above, the meaning of the underlined sentence is:
This is actually the size of the reduced container. The reduced number is the number of deleted elements. Before deleting the element, the destructor of the deleted element will be called
“
In this In some code descriptions following the blog post, when calling vectorerase function, foundvector was called multiple times. According to common understanding, the destructor can be called once to destroy the object, so whyvectorvectorThe container is in useeraseWhen deleting its element object, the destructor of the deleted element object will be called multiple times?
One, What happened?
as in As in the blog post mentioned above, assume that a class is defined as follows:
#include
#include
#include
#include
using namespace std;
class Student
{
public :
Student(const string name = “Andrew”, const int age = 7) : name(name), age(age)
{}
~Student()
{
cout << name << “/tdeleted.” << endl;
}
const string get_name() const
{
return name;
}
const int get_age() const
{
return age;
}
private :
string name;
int age;
};
Then when In the program, like the followingnewExpression(new expression)anddeleteExpression (delete expression), ie
Student *stu = new Student(“Bob” , 7);
and
delete stu;
Behind the end what’s going on?
on top newexpression, actually done3step work:
first Step: Call library functionoperator new, the assignment can accommodate aStudentRaw, untyped memory for type objects;
Second Step: According to the given Actual parameter, callStudent class constructor to construct an object;
Third Step: Return in the second step is constructed>destroyDisplay The destructor of the element object is called in the same way to destroy the object, which is the real reason why the destructor is called multiple times.
on top In the test code, if we put(1)The comment is removed, that is, it is from the beginning VectorObjectsvecallocation can be stored32aStudent object space, then the running result will be:
size:0 capacity:0
size:1 capacity:32
size:2 capacity:32
size:3 capacity:32
size:4 capacity:32
size:5 capacity:32
size:6 capacity:32
size:7 capacity:32
size:8 capacity:32
size:9 capacity:32
Iris deleted.
Howard deleted.
Greg deleted.
Fiona deleted.
Ely deleted.
Dudley deleted.
Chris deleted.
Bob deleted.
Andrew deleted.
We can I saw that almost no destructor was called before, and finally the destructor was called9 times, because of 9aStudentThe result that the object is automatically destroyed when its scope ends. Regarding this point, you can refer to:http://patmusing.blog.163. com/blog/static/13583496020101824142699/
InVectorIn, and did not implementerase, this is becauseeraseTo be usedIterator, due to space limitations, the relevant codes are not listed here, because the principle is the same as the above code, and the above code is enough to explain the blog post http://patmusing.blog.163.com/blog/ static/13583496020101831514657/ about the destructor being called multiple times.
Finally again To emphasize: Explicitly calling the destructor can destroy the object, but it will not release the memory occupied by the object.
an>32
size:9 capacity:32
Iris deleted.
Howard deleted.
Greg deleted.
Fiona deleted.
Ely deleted.
Dudley deleted.
Chris deleted.
Bob deleted.
Andrew deleted.
We can I saw that almost no destructor was called before, and finally the destructor was called9 times, because of 9aStudentThe result that the object is automatically destroyed when its scope ends. Regarding this point, you can refer to:http://patmusing.blog.163. com/blog/static/13583496020101824142699/
InVectorIn, and did not implementerase, this is becauseeraseTo be usedIterator, due to space limitations, the relevant codes are not listed here, because the principle is the same as the above code, and the above code is enough to explain the blog post http://patmusing.blog.163.com/blog/ static/13583496020101831514657/ about the destructor being called multiple times.
Finally again To emphasize: Explicitly calling the destructor can destroy the object, but it will not release the memory occupied by the object.
pt”>Finally, let me emphasize again: calling the destructor explicitly can destroy the object, but it will not release the memory occupied by the object.