stl之map使用.cpp
2017-01-05 23:02
337 查看
/*标准模板库(stl)之映射(map)
*map提供一对一映射,其中第一个键值为主键,不能重复
*map按照主键自动排序
*/
#include <iostream>
#include <map>
using namespace std;
int main()
{
//定义模板类对象并插入元素
map<string, string> words;
words.insert(pair<string,string>("friend", "朋友"));
words.insert({"good", "非常好"});
pair<string,string> a("and", "并且");//定义一个pair对象插入map
words.insert(a);
words.insert({"good", "好"});
words["you"] = "你";//如果有无此映射则添加,如果有则改变
words["friend"] = "哥们";
cout << "查看 good : " << words["good"] << endl;
cout << "查看 yes : " << words["yes"] << endl;//这里没有yes这个key,所以返回为空
//使用迭代器遍历
cout << "--------------" << endl;
for (auto it=words.begin(); it!=words.end(); it++) {
cout << it->first << " : " << it->second << endl;
}
//使用迭代器遍历
map<string ,string>::iterator it = words.find("good");
if (it != words.end()) {
cout << "map is : " << it->second << endl;
} else {
cout << "没找到" << endl;
}
}
*map提供一对一映射,其中第一个键值为主键,不能重复
*map按照主键自动排序
*/
#include <iostream>
#include <map>
using namespace std;
int main()
{
//定义模板类对象并插入元素
map<string, string> words;
words.insert(pair<string,string>("friend", "朋友"));
words.insert({"good", "非常好"});
pair<string,string> a("and", "并且");//定义一个pair对象插入map
words.insert(a);
words.insert({"good", "好"});
words["you"] = "你";//如果有无此映射则添加,如果有则改变
words["friend"] = "哥们";
cout << "查看 good : " << words["good"] << endl;
cout << "查看 yes : " << words["yes"] << endl;//这里没有yes这个key,所以返回为空
//使用迭代器遍历
cout << "--------------" << endl;
for (auto it=words.begin(); it!=words.end(); it++) {
cout << it->first << " : " << it->second << endl;
}
//使用迭代器遍历
map<string ,string>::iterator it = words.find("good");
if (it != words.end()) {
cout << "map is : " << it->second << endl;
} else {
cout << "没找到" << endl;
}
}
相关文章推荐
- STL库之Map结构的使用
- 高效的使用stl::map和std::set
- 使用STL的hash_map要点
- poj 1002 487-3279(使用STL map)
- STL:map的使用:删除map中指定值为value的所有元素
- STL之Map使用小结
- STL 中 map 的使用
- STL_map_使用方法
- C++使用STL中map需要注意的地方
- STL中list,vector,deque,map,set区别、联系和使用场景
- STL中map的简单使用
- C++ STL基本容器的使用(vector、list、deque、map、stack、queue)
- C++ STL 中 map 容器的说明和使用技巧 .
- [转载]C++标准模板库(STL)中map使用详解
- STL hash_map使用
- 【C++】STL中map的详细使用说明
- C++STL map介绍与使用方法
- STL中map容器使用自定义key类型报错详解
- [C++][STL][map]C++ map的基本操作和使用
- 小心使用STL中map的[]操作符