[英]Why "vector.erase()" (in C++) is not behaving as expected?
我写了一个简单的程序来测试“vector.erase”功能。 有一个简单的 class (MyClass0),它在其构造函数和析构函数中写入一些相关消息。 然后是一个向量,其中包含 4 个 MyClass0 类型的对象。 当我擦除向量的第二个元素时:
vec0.erase(vec0.begin() + 1);
我想应该在屏幕上输出消息“GoodBye From 2”。 但是显示消息“GoodBye From 4”。 似乎调用了向量的第 4 个元素的析构函数。 (尽管并非如此,因为当“main”完成时,第 4 个元素将在最后被破坏)。 任何人都可以帮助我,以便我找出原因。 屏幕上显示的代码和 output 是:
代码:
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
class MyClass0
{
public:
MyClass0(int i_i_) : i_(i_i_)
{
cout << "Hello From " << this->i_ << endl;
}
~MyClass0()
{
cout << "GoodBye From " << this->i_ << endl;
}
std::string MyToString()
{
return std::string("This is ") + std::to_string(this->i_);
}
private:
int i_;
};
int main()
{
std::vector<MyClass0> vec0 = { MyClass0(1), MyClass0(2), MyClass0(3), MyClass0(4) };
cout << endl << "Before erasing..." << endl;
vec0.erase(vec0.begin() + 1);
cout << "After erase" << endl << endl;
return 0;
}
屏幕上的Output:
Hello From 1
Hello From 2
Hello From 3
Hello From 4
GoodBye From 4
GoodBye From 3
GoodBye From 2
GoodBye From 1
Before erasing...
GoodBye From 4
After erase
GoodBye From 1
GoodBye From 3
GoodBye From 4
她是你的代码修改了一下
class MyClass0
{
public:
MyClass0(int i_i_) : i_(i_i_)
{
cout << "Hello From " << this->i_ << endl;
}
~MyClass0()
{
cout << "GoodBye From " << this->i_ << endl;
}
std::string MyToString()
{
return std::string("This is ") + std::to_string(this->i_);
}
MyClass0(const MyClass0& other) : i_{other.i_}
{
std::cout << "Copy construct " << i_ << '\n';
}
MyClass0& operator=(const MyClass0& other)
{
std::cout << "Asign " << other.i_ << " onto " << i_ << '\n';
i_ = other.i_;
return *this;
}
private:
int i_;
};
什么暴露了实际发生的事情: https://godbolt.org/z/hW177M7o6
当 vector 从中间删除项目时,它使用operator=
将项目分配给左侧,然后删除最后一个项目。
矢量不允许在中间有任何孔。 这意味着当您删除第二个元素时,您实际上并没有删除它。 发生的情况是所有元素都向前移动以填充孔,之后向量中的最后一个元素可以被删除,因为它已经向前移动了一次
//start with
1 2 3 4
// erase 2, so move 3 into 2 and 4 into 3
1 3 4 *
// * is old 4 and we don't need that so remove it from the collection
1 3 4
// removing * calls the destructor for that element
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.