利用程序演示相关内容,构建unordered_map<string,string> mymap。
mymap中的键值对为:国家-首都
// unordered_map::erase
#include <iostream>
#include <string>
#include <unordered_map>
int main ()
{
std::unordered_map<std::string,std::string> mymap;
// populating container:
mymap["U.S."] = "Washington";
mymap["U.K."] = "London";
mymap["France"] = "Paris";
mymap["Russia"] = "Moscow";
mymap["China"] = "Beijing";
mymap["Germany"] = "Berlin";
mymap["Japan"] = "Tokyo";
// erase examples:
mymap.erase ( mymap.begin() ); // 根据迭代器进行移除
mymap.erase ("France"); // 根据键进行移除
mymap.erase ( mymap.find("China"), mymap.end() ); // 移除某一范围
// show content:
for ( auto& x: mymap )
std::cout << x.first << ": " << x.second << std::endl;
return 0;
}
得到的结果为:
Russia: Moscow
Japan: Tokyo
U.K.: London
|