向量擦除调用了错误的析构函数

Ema*_*llo 2 c++

我有一些物体的向量。我注意到,当我使用擦除方法从向量中删除一个元素时,我得到了对错误元素的析构函数调用(总是指向最后一个元素)。这是产生不良输出的最小示例。

// Example program
#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Test {
    public:
        Test(string value) {
            _value = value;
            cout << "Constructor:" << _value << endl;
        }
        Test(const Test &t) {
            _value = t._value;
            cout << "Copied:" << _value << endl;
        }
        ~Test() {
            cout << "Destructor:" << _value << endl;
        }
        string print() {
            return _value;
        }
        string _value;
};

int main()
{
    vector<Test> vec;
    vec.reserve(3);
    cout << "Creation" << endl << endl;
    vec.push_back(Test("1"));
    vec.push_back(Test("2"));
    vec.push_back(Test("3"));

    cout << endl << "Deleting" << endl << endl;

    vec.erase(vec.begin());     // Here is called the element with value "3"
    vec.erase(vec.begin());     // Here is called the element with value "3"

    cout << endl << "Log" << endl << endl;

    // But the final log print "3"
    for (unsigned i = 0; i < vec.size(); i++) {
        cout << vec[i].print()<<endl;
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出为:

Creation

Constructor:1
Copied:1
Destructor:1
Constructor:2
Copied:2
Destructor:2
Constructor:3
Copied:3
Destructor:3

Deleting

Destructor:3         <-- WRONG, NEED TO BE 1
Destructor:3         <-- WRONG, NEED TO BE 2

Log

3
Destructor:3
Run Code Online (Sandbox Code Playgroud)

我将解决此问题而无需更改容器向量。

Que*_*tin 5

vec.erase(vec.begin());没有破坏的第一要素。它通过使用move-or或copy-assignment运算符将所有后续位移动一位,从而将其覆盖。从中移出最后一个元素后剩下的东西将被销毁,这就是您要观察的内容。

  • OP,如果您添加一个`operator =()`方法,该方法在调用时记录日志,它将更加清晰。 (3认同)