能用,但不能保证数据,removed后最好clear下。 Reusing a moved container?
#include <iostream>
#include<map>
using namespace std;
class A {
public:
map<int, int> a;
bool b = false;
};
int main()
{
A t;
t.a[1] = 1;
A x;
cout << "t: " << t.a.size() << " " << t.b <<endl;
x.a = std::move(t.a);
t.b = true;
cout << "moved t: " << t.a.size() << " " << t.b << endl;
cout << "x: " << x.a.size() << " " << x.b << " " << x.a[1] << endl;
t.a[2] = 2;
cout << "t: " << t.a.size() << " " << t.b << " " << t.a[2] << endl;
return 0;
}
t: 1 0
moved t: 0 1
x: 1 0 1
t: 1 1 2