When vector deletes elements with erase, why is the destructor of the deleted element called more times? _Blog
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()…