multimap

1.元素的添加和删除

添加

authors.insert(make_pair(string("Barth, John"), string("Sot-Weed Factor")));

authors.insert(make_pair(string("Barth, John"), string("Lost in the Funhouse"))) ;

删除


带有一个键参数的erase版本将删除拥有该键的所有元素,并返回删除元素的个数。而带有一个或一对迭代器参数的版本只删除指定的元素,并返回void类型:

multimap<string, string> authors ;

string search_item("Kazuo Ishiguro") ;

// erase all elments with this key;returns number of elements removed ;

multimap<string, string>::size_type cnt = authors.erase(search_item) ;


2.查找元素

1> 使用find和count操作

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>与众不同的面向迭代器的解决方案

typedef multimap<string, string>::iterator authors_it ;

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>equal_range函数

pair<authors_it, authors_it> pos = authors.euqal_range(search_item) ;

while(pos.first != pos.second)

{

cout << pos.first->second << endl ;

++pos.first ;

}

你可能感兴趣的:(multimap)