map<int, shared_ptr<int>>map_test; shared_ptr<int> tmp_1 = make_shared<int>(1); map_test[1] = tmp_1; shared_ptr<int> tmp_20 = make_shared<int>(20); shared_ptr<int> tmp_vice1 = map_test[1]; tmp_vice1.swap(tmp_20);
今天錯誤的使用了swap 如以上代碼,本意是想改變map_test[1]所對應的值為20
可是這樣使用后 是把tmp_vice1指向了原先tmp_20所管理的內存,tmp_20指向了 tmp_1管理的內存 而不是內存的相互交換!
正確使用直接用map_test[1] 和tmp_20 swap即可