STL_map用法
来源:互联网 发布:手机淘宝自分类链接 编辑:程序博客网 时间:2024/05/21 10:07
一、map容器简介:
map是关联容器,存储按照特定顺序组合键值(key)和映射值(value)组合的元素。在地图中,键值通常用于对元素进行排序和唯一标识,而映射的值存储与该键相关联的内容。键和映射值的类型可能不同。(map底层通常实现为二叉搜索树)**
二、构造函数用法
<1>构造一个空容器:
map<char, int> map1; //map<string, string> map1; //map<char, string> map1; //map<int, int> map1;
<2>给定一个已存在map的两个边界迭代器,来构造新的map
map<char, int> map1; map1['a'] = 10; map1['b'] = 20; map1['c'] = 30; map1['d'] = 40; //range constructor //用已存在的map,给定范围构造新的map // map (InputIterator first, InputIterator last) map<char, int> map2(++map1.begin(), map1.end()); for (map<char, int>::iterator it2 = map2.begin(); it2 != map2.end(); it2++) { cout << it2->first << " " << it2->second << endl; }
输出结果:
b 20
c 30
d 40
<3>用已存在的map拷贝构造新的map
//拿已存在的map拷贝构造新map //copy constructor //map (const map& x); map<char, int> map3(map2); for (map<char, int>::iterator it3 = map3.begin(); it3 != map3.end(); it3++) { cout << it3->first << " " << it3->second << endl; } //输出结果和map2相同
三、插入(insert)用法
<1>单个元素插入:
函数原型为:pair < iterator,bool> insert (const value_type& val);如果成功插入了新元素,则pair中的第二个元素为bool;反之为false;
map<char, int> map1; pair<map<char, int>::iterator, bool> ret; ret = map1.insert(pair<char, int>('e', 50)); if (ret.second == false) cout << "插入失败" << endl; for (map<char, int>::iterator it1 = map1.begin(); it1 != map1.end(); it1++) { cout << it1->first << " " << it1->second << endl; } cout << endl;
输出结果:
插入成功
e 50
<2>在给定迭代器位置进行插入:
函数原型为:iterator insert (iterator position, const value_type& val);返回一个指向新插入的元素的迭代器。
map<char, int> map1; map1.insert(pair<char, int>('a', 2)); map1.insert(pair<char, int>('z', 2)); map<char, int>::iterator it2 = map1.begin(); map1.insert(it2, pair<char, int>('b', 15)); //map1.insert(it2, pair<char, int>('x', 16)); for (map<char, int>::iterator it1 = map1.begin(); it1 != map1.end(); it1++) { cout << it1->first << " " << it1->second << endl; }
输出结果:
a 2
b 15
z 2
<3>在给定迭代器位置进行插入:
函数原型为:void insert(InputIterator first, InputIterator last); 用已存在map的两个边界迭代器来构造新的map。(注:注意边界是左闭右开)
map<char, int> map1; map1.insert(pair<char, int>('b', 2)); map1.insert(pair<char, int>('c', 2)); map1.insert(pair<char, int>('a', 2)); map1.insert(pair<char, int>('z', 2)); map<char, int> mymap; mymap.insert(map1.begin(), map1.find('c')); for (map<char, int>::iterator it1 = mymap.begin(); it1 != mymap.end(); it1++) { cout << it1->first << " " << it1->second << endl; }
输出结果:
a 2
b 2
四、删除(erase)用法
<1>删除给定迭代器位置的元素:
函数原型为:void erase (iterator position);
//1.erasing by iterator map<int, int> map1; map1[0] = 1; map1[1] = 2; map1[2] = 3; map1[5] = 5; map1[8] = 7; map<int, int>::iterator it1 = map1.find(1); map1.erase(it1);
<2>给定key值,进行删除操作:
函数原型为:size_type erase (const key_type& k);函数返回已擦除元素的数量。
map1.erase(2);//删除key为2的元素
<3>给定要删除的范围,进行删除操作:
函数原型为:void erase (iterator first, iterator last)
map<int, int>::iterator first = map1.find(5); map1.erase(first, map1.end()); for (map<int, int>::iterator start = map1.begin(); start != map1.end(); start++) cout << start->first << "," << start->second << endl;
输出结果:
0,1
1,2
五、查找(find)用法
查找map中key与要查找的值相等的元素,返回对应迭代器。函数原型为: iterator find (const key_type& k);
map<string, string> map1; map1.insert(pair<string, string>("sort", "排序")); map1.insert(pair<string, string>("hello", "你好")); map<string, string>::iterator tmp = map1.find("sort"); cout << "查询结果:"<<tmp->second << endl;
输出结果:
查询结果:排序
六、map::operator[]用法
函数原型为:mapped_type& operator[] (const key_type& k);如果k与容器中的元素的key匹配,函数将返回对其value值的引用;如果k与容器中任何元素的key不匹配,则该函数将使用该k插入一个新元素,并返回对其value值的引用。 (该元素使用其默认构造函数构造)。
map<int, char> map1; //一开始map1容器为空,没有key与0相匹配,则插入此元素 map1[0] = 'a'; map1[1] = 'b'; //存在map1[0],返回value的引用,可直接进行修改 map1[0] = 'c'; for (map<int, char>::iterator it = map1.begin(); it != map1.end(); it++) { cout << it->first << " " << it->second << endl; }
输出结果:
0 c
1 b
补充:由于map是许多键值对的组合,所以简单介绍下pair。通常有一下几种定义和初始化方法:
pair<T1, T2> p; pair<T1, T2> p(v1, v2); make_pair(v1, v2)
想取出一个pair对中的值就需要使用pair.first取得第一个值;使用pair.second来获取第二个值。
更详细的接口使用方法参考http://www.cplusplus.com/reference/map/map/
阅读全文
1 0
- STL_map用法
- STL_map用法
- stl_map,set 用法
- STL_Map用法详解
- STL_map
- STL_map
- STL_MAP
- STL_map
- STL_map
- stl_map.h
- STL_map操作
- STL_MAP容器常用操作
- poj2503-Babelfish-STL_map
- 《STL源码剖析》-- stl_map.h
- STL_map——map::begin
- STL_map——map::clear
- STL_map——map::count
- STL_map——map::empty
- Collection 集合
- 函数调用堆栈
- C++实现二叉树的镜像操作(18)---《那些奇怪的算法》
- 【安全牛学习笔记】主动探测
- leetcode007-3Sum Closest
- STL_map用法
- 设计模式之单例模式(线程安全)
- 发明专利申请过程及案例下载
- JavaSwing_5.1: 组件的位置和尺寸
- Hibernate 缓存机制
- 总结积累
- 超实用表单验证方法!拿去不谢
- java程序员面试书目录
- 从零学习Belief Propagation算法(三)