考慮下面的代碼:std :: map make_pair - 如何僅在密鑰未列出時更改配對(否則更新密鑰)?
std::map <string,string> myMap;
myMap.insert(std::make_pair("first_key" , "no_value"));
myMap.insert(std::make_pair("first_key" , "first_value"));
myMap.insert(std::make_pair("second_key" , "second_value"));
typedef map<string, string>::const_iterator MapIterator;
for (MapIterator iter = myMap.begin(); iter != myMap.end(); iter++)
{
cout << "Key: " << iter->first << endl << "Values:" << iter->second << endl;
}
輸出是:
Key: first_key
Values:no_value
Key: second_key
Values:second_value
含義是,第二次分配:
myMap.insert(std::make_pair("first_key" , "first_value"));
未能成行。
只有鑰匙還沒有列出,如果列出 - 如何更改其值?
是否有任何std :: map的通用方法?
你爲什麼需要'make_pair'?我的意思是,更傳統的'myMap [「first_key」] =「first_value」'有什麼問題? – dasblinkenlight