public member function
<map>
std::multimap::lower_bound
iterator lower_bound (const key_type& k);const_iterator lower_bound (const key_type& k) const;
Return iterator to lower bound
返回一個迭代器,指向容器中第一個鍵值不小於k的元素(即,等於k或大於k)。
該函式使用其內部的比較物件(key_comp)來確定這一點,返回第一個使得key_comp(element_key,k)返回false.
的元素的迭代器。如果multimap類使用預設比較型別(less)進行例項化,則該函式返回第一個鍵值不小於k的元素的迭代器。
一個類似的成員函式,upper_bound,其行為與lower_bound相同,只是在multimap包含鍵值與k相等的元素時:在這種情況下,lower_bound返回指向這些元素中第一個元素的迭代器,而upper_bound返回指向最後一個元素之後那個元素的迭代器。
引數
- k
- 要搜尋的鍵。
成員型別key_type是容器中元素的型別,在map中定義為其第一個模板引數(鍵).
返回值
指向容器中第一個鍵值不小於k的元素的迭代器,如果所有鍵值都小於k,則返回multimap::end。
如果multimap物件是const限定的,則該函式返回一個const_iterator。否則,它返回一個iterator.
成員型別iterator和const_iterator是指向元素的(型別為value_type).
請注意,value_type的multimap容器中的pair<const key_type, mapped_type>.
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
// multimap::lower_bound/upper_bound
#include <iostream>
#include <map>
int main ()
{
std::multimap<char,int> mymultimap;
std::multimap<char,int>::iterator it,itlow,itup;
mymultimap.insert(std::make_pair('a',10));
mymultimap.insert(std::make_pair('b',121));
mymultimap.insert(std::make_pair('c',1001));
mymultimap.insert(std::make_pair('c',2002));
mymultimap.insert(std::make_pair('d',11011));
mymultimap.insert(std::make_pair('e',44));
itlow = mymultimap.lower_bound ('b'); // itlow points to b
itup = mymultimap.upper_bound ('d'); // itup points to e (not d)
// print range [itlow,itup):
for (it=itlow; it!=itup; ++it)
std::cout << (*it).first << " => " << (*it).second << '\n';
return 0;
}
|
b => 121
c => 1001
c => 2002
d => 11011
|
資料競爭
訪問容器(const 和非 const 版本都不會修改容器)。
不訪問對映值:併發訪問或修改元素是安全的。
異常安全
強保證:如果丟擲異常,容器沒有發生變化。