c++ map 记录
2015-08-11 16:37
239 查看
map
map是一种关系型容器,必须包含两种元素,一个是关键字,另一个是值。map有两种,一种是一般的map对象,它是一对一对应关系;另外一种是multimap,这中类型是可以允许一对多的关系。这两中map类型除此之外没有其他的区别。
头文件: <map>
map的定义:
map <type, type> map对象名
或者是typedef map <type, type> 新的map对象类型名 (这里假设是MyMap)
typedef map<int, string> MyMap;
MyMap myMap;
map的增添操作:(前三种返回的是一个pair <iterator, bool>,不能重复插入相同的值,第四种是直接将原位置的值覆盖)
方法一:
myMap.insert(pair <int, "string">(1, "google1"));
(1) my_Map["a"] = 1;
myMap.insert(pair<int,
"string">(2, "google2"));
方法二:
myMap.insert(make_pair <int, string> (3,"google3"));
myMap.insert(make_pair <int, string> (4,"google4"));
方法三:
myMap.insert(map <int, string>::value_type(5, "google5"));
myMap.insert(map <int, string>::value_type(6, "google6"));
方法四:
myMap[7] ="google7";
map的遍历:
for (map<int, string>::iterator it = myMap.begin(); it != myMap.end(); ++it){
cout << it->first << " "<< it->second;
}
map的删除:while(){
map<int, string>::iterator it = myMap.begin();
cout << it->first << " " << it-> second << endl;
myMap.erase(it);
}
map是一种关系型容器,必须包含两种元素,一个是关键字,另一个是值。map有两种,一种是一般的map对象,它是一对一对应关系;另外一种是multimap,这中类型是可以允许一对多的关系。这两中map类型除此之外没有其他的区别。
头文件: <map>
map的定义:
map <type, type> map对象名
或者是typedef map <type, type> 新的map对象类型名 (这里假设是MyMap)
typedef map<int, string> MyMap;
MyMap myMap;
map的增添操作:(前三种返回的是一个pair <iterator, bool>,不能重复插入相同的值,第四种是直接将原位置的值覆盖)
方法一:
myMap.insert(pair <int, "string">(1, "google1"));
(1) my_Map["a"] = 1;
myMap.insert(pair<int,
"string">(2, "google2"));
方法二:
myMap.insert(make_pair <int, string> (3,"google3"));
myMap.insert(make_pair <int, string> (4,"google4"));
方法三:
myMap.insert(map <int, string>::value_type(5, "google5"));
myMap.insert(map <int, string>::value_type(6, "google6"));
方法四:
myMap[7] ="google7";
map的遍历:
for (map<int, string>::iterator it = myMap.begin(); it != myMap.end(); ++it){
cout << it->first << " "<< it->second;
}
map的删除:while(){
map<int, string>::iterator it = myMap.begin();
cout << it->first << " " << it-> second << endl;
myMap.erase(it);
}
相关文章推荐
- C语言大数乘法
- C++控制程序只运行一个实例
- C++11 委派构造函数特性怎么使用?
- C语言中,为什么字符串可以赋值给字符指针变量
- C#与C++数据类型对应表
- C#与C++数据类型比较及结构体转换(搜集整理二)
- Music
- 快速排序(C++版)
- C#调用C++的DLL搜集整理的所有数据类型转换方式
- C或C++输入缓冲区问题
- C语言与汇编混合编程
- 黑马程序员——C语言笔记之循环结构
- [LeetCode] Convert Sorted Array to Binary Search Tree
- C语言常用字符串库函数实现
- C++基础知识点
- [LeetCode] Construct Binary Tree from Preorder and Inorder Traversal
- Here's what C++ is
- 智能指针的使用
- C++ Primer 学习笔记_38_STL实践与分析(12)--集成的应用程序容器:文本查询程序
- 黑马程序员——C语言笔记之scanf函数及使用注意事项