STL 映射“[]”运算符可以插入新条目或修改现有条目。
map<string, string> myMap;
myMap["key1"] = "value1";
myMap["key1"] = "value2";
我正在用 STL map 实现的 boost::bimap 重写一些代码。有没有一种简单的方法来保持 STL“[]”行为?我发现我必须写下 7 行代码来替换原始的 STL 映射代码(1 行!)。
bimap<string, string>::left_iterator itr = myBimap.left.find("key1");
if (itr != myBimap.left.end()) {
myBimap.left.replace_data(itr, "value2");
}
else {
myBimap.insert(bimap<string, string>::value_type("key1", "value2"));
}
我想知道是否有像 boost::bimap::insert_or_modify() 这样的实用函数。