关联容器

关联容器

1.map类型

map定义的类型:
map<K, V>::key_type   //在map容器中用作索引的键的类型
map<K, V>::mapped_type   //在map容器中用作值的类型
map<K, V>::value_type   //一个pair类型,它的first元素具有const map<K, V>::key_type类型,而second元素具则为map<K, V>::mapped_type类型

1.1map迭代器进行解引用产生pair类型的对象

对迭代器进行解引用获得一个 pair类型对象,它的first成员存放键,为const,因此键一旦生成便不能更改
map<string, int> word_count;
 it->first = "new key";//出错,键不能更改
 map<string, int>::iterator it = word_count.begin();

1.2使用下标访问访问map对象

使用下标访问map与使用下标访问数组或vector截然不同:用下标访问不存在的元素将导致在map容器中添加一个新的元素,它的键即为该下标值

1.3查找并读取map中的元素

1.3.1使用count检查map对象中某键是否存在
count成员返回值只能为0或1,因为map容器只允许一个键对应一个实例。
1.3.2读取元素而又不插入元素
find操作返回只想元素的迭代器,如果元素不存在,返回end迭代器。
因此,判断元素存在用count,如果希望当元素存在时就是用它,则使用find操作,然后使用它返回的迭代器进行操作。

2.set类型

set类型中只能保存键不同的元素,同样建不能修改,为const类型。

3.multimap和multiset类型

multmap和multset类型允许一个键对应多个实例,但是 multmap不支持下标运算,因为一个键可能对应多个值。

3.1元素的添加和删除

一个作者可以写多本书:
multimap<string, string> authors;
authors.insert(make_pair(string("Barth, John"), string("Sot-weed Factor")));
authors.insert(make_pair(string("Barth, John"), string("Lost in the Funhouse")));//正确,一个作者写了两本书
带有一个键参数的erase版本将删除拥有该键的所有元素,并返回删除元素的个数。而带有一个或者一对迭代器参数的版本只删除指定的元素。
3.2在multimap和multiset中查找元素
用例:某键对应的元素可能出现多次(假设有作者与书名的映射,找到并输出某个作者的所有书的书名)
3.2.1使用find和count操作
multimap<string, string> authors;
authors.insert(make_pair(string("name"), string("123")));
authors.insert(make_pair(string("name"), string("456")));
authors.insert(make_pair(string("name"), string("789")));
string search_item("name");
typedef multimap<string ,string>::size_type sz_type;//定义类型
sz_type entries = authors.count(search_item);
multimap<string, string>::iterator iter = authors.find(search_item);//根据cnt来递增迭代器
for(sz_type cnt=0; cnt!=entries; ++cnt, ++iter)
	cout<<iter->second<<endl;//输出值
3.2.2面向迭代器的解决方案
使用关联容器操作:lower_bound和upper_bound: 在同一个键上调用lower_bound和upper_bound将产生一个迭代器范围,指示该键所关联的所有元素(注意可能返回超出末端的迭代器)
multimap<string, string> authors;
authors.insert(make_pair(string("name"), string("123")));
authors.insert(make_pair(string("name"), string("456")));
authors.insert(make_pair(string("name"), string("789")));
typedef multimap<string, string>::iterator authors_it;
string search_item("name");//要查找的键
authors_it beg = authors.lower_bound(search_item);//获取低端迭代器
authors_it end = authors.upper_bound(search_item);//获取高端迭代器
while(beg != end)//遍历
{
	cout<<beg->second<<endl;//输出值
	++beg;//递增迭代器
}

3.2.3equsl_range函数来解决
调用equal_range函数来取代调用upper_bound和lower_bound函数。equal_range返回存储一对迭代器的pair对象。 如果该值存在,则pair对象中的第一个迭代器指向该键关联的第一个实例,第二个迭代器指向该键关联的最后一个实例的下一个位置
multimap<string, string> authors;
authors.insert(make_pair(string("name"), string("123")));
authors.insert(make_pair(string("name"), string("456")));
authors.insert(make_pair(string("name"), string("789")));
typedef multimap<string, string>::iterator authors_it;
string search_item("name");//要查找的键
pair<authors_it, authors_it> pos = authors.equal_range(search_item);//获取迭代器范围
while(pos.first != pos.second)//循环
{
	cout<<pos.first->second<<endl;//输出第一个迭代器里面的值
	++pos.first;//迭代器自增



就到这里吧,看了STL源码剖析在进一步分析STL的强大。

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值