STL源码:multimap、multiset
来源:互联网 发布:南昌市网络教研平台 编辑:程序博客网 时间:2024/06/05 06:05
multiset和multimap与set、map的特性以及用法完全相同,差别之一是它允许一个键对应多个实例,因为它们的插入操作使用的是底层RB-tree的insert_equal() 而不是insert_unique()函数。第二个差别multimap不支持下标运算,而map支持。
在电话薄中,每个人可能对应多个电话号码;作家会有多本对应的著作;这都需要用multimap来表示。
元素插入删除
insert操作和 erase操作同样也适用于multimap以及 multiset容器。只是不同的是:由于键不要求是唯一的,因此每次调用insert总会添加一个元素。
multimap<string,string> authors; authors.insert(make_pair("Barth John","Sot-Weed Factor")); authors.insert(make_pair("Barth John","Test"));
带有一个键参数的erase版本将删除拥有该键的所有元素,并返回删除的个数。而带有一个或一对迭代器参数的版本只删除指定的元素,并返回void类型。
multimap<string,string> authors; authors.insert(make_pair("Barth John","Sot-Weed Factor")); authors.insert(make_pair("Barth John","Sot-Weed Factor")); authors.insert(make_pair("Barth John","Sot-Weed Factor")); string searchStr("Barth John"); multimap<string,string>::size_type cnt = authors.erase(searchStr); cout << cnt << endl;//cnt == 3
查找元素
关联容器nap和set的元素是按照顺序存储的,而multimap和multiset也一样。因此,在multiset和multim容器中,如果某个键对应于多个实例,则这些实例在容器中必然相邻存放。 因此,在遍历multimap/multiset容器时,可以保证依次返回特定键所关联的所有元素。
1、使用find和count
使用count函数可以求出某个键出现的次数,而find操作将返回一个迭代器,指向第一个拥有正在查找的键的实例。
string search_item("Alain de Botton"); typedef multimap<string,string>::size_type sz_type; sz_type entries = authors.count(search_item); multimap<string,string>::iterator iter = authors.find(search_item); for (sz_type cnt = 0;cnt != entries; ++cnt,++iter) { cout << iter -> second << endl; }
2、面向迭代器的解决方案
下表列出的这些操作适用于所有的关联容器,也可用于普通的map和set容器,但是更常用于multiamp和multiset容器。所有的这些操作都需要一个键,并返回一个迭代器。
返回迭代器的关联容器操作
m.lower_bound(k)
返回一个迭代器,指向键不小于k的第一个元素
m.upper_bound(k)
返回一个迭代器,指向键大于k的第一个元素
m.equal_range(k)
他的first成员等价于m.lower_bound(k),
second成员等价于m.upper_bound(k)
参考这里的
typedef multimap<string,string>::iterator authors_it; string searchVal = "Barth John"; authors_it beg = authors.lower_bound(searchVal), end = authors.upper_bound(searchVal); while (beg != end) { cout << beg -> second << endl; ++ beg; }
3、equal_range函数
可用通过调用equal_range函数来取代调用upper_bound和lower_bound函数。equal_range函数返回存储一对迭代器的pair对象。如果该值存在,则pair对象中的第一个迭代器指向该键关联的第一个实例,第二个迭代器指向该键关联的最后一个实例的下一位置。如果找不到匹配的元素,则pair对象中的两个迭代器都将指向此键应该插入的位置。
pair<authors_it,authors_it> pos = authors.equal_range(searchVal); while (pos.first != pos.second) { cout << pos.first -> second << endl; ++ pos.first; }
- STL源码:multimap、multiset
- stl之multiset,multimap
- STL源码剖析之map set multimap multiset【2013.12.10】
- C++ STL multimap multiset 简介
- STL容器之multimap和multiset
- STL map, multimap, set, multiset 函数介绍
- STL关联容器之multiset和multimap
- STL容器 set、 multiset、map、multimap
- C++ STL之 map\set\multimap\multiset
- 【STL源码剖析读书笔记】【第5章】关联式容器之set、map、multiset和multimap
- STL源码剖析-关联式容器之set,map,multiset和multimap
- STL — 从源码层面区别map set和multiset multimap
- 【STL】STL中红黑树的应用set、multiset、map、multimap
- STL 之 multimap 源码剖析
- STL概览-关联容器set,multiset,map,multimap(四)
- STL set multiset map multimap unordered_set unordered_map example
- stl中的map、set、multimap、multiset,兼谈OceanBase造轮子
- STL中HashTable HashMap HashSet Set Map MultiSet MultiMap总结
- 去掉最大值、最小值之后剩下的个数
- 后缀自动机:O(N)的构建及应用
- linux-2.6.32在mini2440开发板上移植(8)之添加ADC驱动程序
- String,StringBuffer与StringBuilder的区别??
- 使用TestNG-xslt
- STL源码:multimap、multiset
- 程序员2014精华本
- jqGrid动态隐藏列显示列以及设置显示相对宽度
- 依赖和爱
- 一些开源数据库问题及其配置文件地址
- axis1.0+带证书验证方式访问webservice的问题
- cocoaPods安装及使用方法
- UVA - 1484(树形dp,最大值最小值同时维护)
- 在ie上使用xpath时selenium运行缓慢