void TestMap() { map<string, int> m; m.insert(pair<string, int>("sort", 1)); m.insert(pair<string, int>("sort", 2)); m.insert(pair<string, int>("quick", 5)); m.insert(pair<string, int>("hash", 1)); m.insert(pair<string, int>("map", 1)); m.insert(pair<string, int>("set", 1)); map<string, int>::iterator it = m.begin(); while (it != m.end()) { cout<<"key:"<< it->first.c_str() << " value:"<<it->second<<endl; ++it; } cout << endl; }
模拟字典
typedef map<string, string> DictMap; typedef DictMap::iterator DictMapIter; map<string, string> dict; dict.insert(pair<string, string>("sort","排序")); dict.insert(pair<string, string>("hash", "哈希")); dict.insert(pair<string, string>("test", "测试")); dict.insert(pair<string, string>("erase", "错误")); /*pair<DictMapIter, bool> ret = dict.insert(pair<string, string>("erase", "删除"));*/ //修改value的两种方式 //1. //DictMapIter ret = dict.find("erase"); //if (ret != dict.end()) //{ // //ret->first = "delete"; // ret->second = "删除"; //} //2 dict["erase"] = "删除"; dict["erase"] = "错误"; dict["delete"]; //T() dict["delete"] = "删除";
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。