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()…

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索