Fir*_*cer 7 c++ reference stdmap
我是否正确地假设向std :: map添加/删除元素不会影响其他元素(即使它们在内存中重新定位),以便以下是安全的:
我查看了有关容器信息的各个站点,但只发现了迭代器失效的情况,我已经知道...
std::map<std::string,std::string> map;
PopulateMap(map);
std::string &a= map["x"];
AddMoreData(map);
RemoveRandomKeysExceptX(map);
map["x"] = "foo";
std::cout << a << " " << map["x"] << std::endl;//prints "foo foo"
a = "bar";
std::cout << a << " " << map["x"] << std::endl;//prints "bar bar"
Run Code Online (Sandbox Code Playgroud)
我在VC9上测试了一些类似的代码,但这似乎不起作用,但这并不意味着我不仅仅是幸运,也不是因为编译器没有变化.