I have written a simple program to test "vector.erase" feature. There is a simple class (MyClass0) which writes some related message in it's constructor and another in it's destructor. And then there is a vector which contains 4 objects of type MyClass0. As I erase the second element of the vector:
vec0.erase(vec0.begin() 1);
I suppose that the Message "GoodBye From 2" should be outputted on the screen. But the message "GoodBye From 4" is shown. It seems that the destructor of the 4'th element of the vector is called. (Although it is not the case, because the 4'th element will be destructed at the end, when the "main" is finished). can anyone help me please so that I can find out the reason. The code and the output which is shown on the screen are:
Code:
#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 on the screen:
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
https://godbolt.org/z/qvrcb81Ma
CodePudding user response:
Her is your code modified a bit
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_;
};
What exposes what actually happens: https://godbolt.org/z/hW177M7o6
When vector removes item from a middle it assigns items using operator=
shifting them to the left and then deletes last item.
CodePudding user response:
A vector is not allowed to have any holes in the middle. That means when you erase the second element you don't actually remove it. What happens is all of the elements get moved forward to fill in the hole, and after that the last element in the vector can be removed as it has been moved forward once
//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