公有成員函式
<map>

std::map::lower_bound

      iterator lower_bound (const key_type& k);const_iterator lower_bound (const key_type& k) const;
返回指向下界的迭代器
返回一個迭代器,指向容器中第一個其鍵值不被認為在 k 之前的元素(即,該鍵值要麼等於 k,要麼在 k 之後)。

該函式使用其內部的 比較物件 (key_comp) 來確定這一點,並返回第一個滿足以下條件的元素的迭代器:key_comp(element_key,k)返回false.

如果 map 類使用預設的比較型別 (less) 例項化,則該函式返回第一個鍵值不小於 k 的元素的迭代器。

一個類似的成員函式 upper_bound 的行為與此相同,lower_bound不同之處在於 map 包含一個鍵值等於 k 的元素:在這種情況下,lower_bound返回指向該元素的迭代器,而 upper_bound 返回指向下一個元素的迭代器。

引數

k
要搜尋的鍵。
成員型別key_type是容器中元素的型別,在map中定義為其第一個模板引數().

返回值

一個迭代器,指向容器中第一個其鍵值不被認為在 k 之前的元素,如果所有鍵值都被認為在 k 之前,則返回 map::end

如果 map 物件是 const 限定的,則該函式返回一個const_iterator。否則,它返回一個iterator.

成員型別iteratorconst_iterator是指向元素的(型別為value_type).
請注意,value_typemap 容器中,本身也是一個 pair 型別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
26
// map::lower_bound/upper_bound
#include <iostream>
#include <map>

int main ()
{
  std::map<char,int> mymap;
  std::map<char,int>::iterator itlow,itup;

  mymap['a']=20;
  mymap['b']=40;
  mymap['c']=60;
  mymap['d']=80;
  mymap['e']=100;

  itlow=mymap.lower_bound ('b');  // itlow points to b
  itup=mymap.upper_bound ('d');   // itup points to e (not d!)

  mymap.erase(itlow,itup);        // erases [itlow,itup)

  // print content:
  for (std::map<char,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
    std::cout << it->first << " => " << it->second << '\n';

  return 0;
}

a => 20
e => 100


複雜度

size 的對數複雜度。

迭代器有效性

沒有變化。

資料競爭

訪問容器(const 和非 const 版本都不會修改容器)。
不訪問對映值:併發訪問或修改元素是安全的。

異常安全

強保證:如果丟擲異常,容器沒有發生變化。

另見