我有两个地图。两个地图的密钥是相同的。第二个的映射值是一个指针,它指向第一个的映射值。当我擦除第一张地图中的元素时,第二张地图中的指针不会自动消失。我应该先删除第二张地图,然后再删除第一张地图。
// two maps
map<int, int> a;
map<int, int*> pt_a;
int N = 5;
for (size_t i = 0; i < N; i++)
{
a.insert({ i,2 * i });
pt_a.insert({ i,&(a[i]) });
}
// erase the first element of a
a.erase(a.begin());
// after erase
for (auto& i : a) cout << i.first << " " << &(i.second) << endl;
cout << endl;
for (auto& i : pt_a) cout << i.first << " " << i.second << endl;
std::share_ptr
在这种情况下?谢谢!