map
红黑树实现-红黑树算法和应用(更高级的二叉查找树) map 存储的是 pair 对象 按照key的大小 按照既定规则排序(默认是升序排序)
- std::make_pair
- insert
- emplace 插入
- 遍历
- [] 获取元素 找不到为 该类型的默认值 string “” int 0
- 重复的就是更新
user.insert( user.begin(),std::make_pair("STL教程","http://baidu.com"));
insert
user.insert( user.begin(),std::make_pair("STL教程","http://baidu.com"));
pair<map<string,string>::iterator, bool> temp = user.emplace(std::make_pair("STL教程", "http://baidu.com"));
temp -> <iterator,bool>
bool flag = temp.second;
false
emplace
string str = "Lionel";
if (user.find(str) == user.end())
{
printf("can not found.");
}
printf("exist");
find
for (auto i = user.begin(); i != user.end(); ++i) {
printf("%s--%s \n", i->first.c_str(), i->second.c_str());
}
OR
for (auto i = user.begin(); i != user.end(); ++i) {
cout << i->first << " " << i->second << endl;
}
这么写会错
for (auto i = user.begin(); i != user.end(); ++i) {
printf("%s--%s \n", i->first, i->second);
}
遍历
map可以这样,multimap就不行。
map<string, string> user;
multimap<string, string>user_m;
user["c"] = "Lionel";
multimap
multimap可以存储重复key的pair
string name[6] = { "a","a" ,"b","c","d","e"};
string password[6] = { "12345" ,"00909","kitty","bitch","7890","6789"};
for (int i = 0;i < 5;i++)
{
user_m.insert(make_pair(name[i],password[i]));
}
或者
string name[6] = { "a","a" ,"b","c","d","e"};
string password[6] = { "12345" ,"00909","kitty","bitch","7890","6789"};
for (int i = 0;i < 5;i++)
{
user_m.insert(pair<string, string >(name[i],password[i]));
}
|