公共成員函式
<map>

std::map::equal_range

pair<const_iterator,const_iterator> equal_range (const key_type& k) const;pair<iterator,iterator>             equal_range (const key_type& k);
獲取相等元素的範圍
返回容器中所有鍵等於 k 的元素的範圍的邊界。

由於 map 容器中的元素具有唯一的鍵,因此返回的範圍最多包含一個元素。

如果未找到匹配項,則返回的範圍長度為零,兩個迭代器均指向第一個根據容器的 內部比較物件 (key_comp) 被認為大於 k 的元素。

當容器的 比較物件 返回false時,兩個 被視為等價(即,無論鍵按何種順序作為引數傳遞)。

引數

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

返回值

該函式返回一個 pair,其成員pair::first是範圍的下界(與 lower_bound 相同),並且pair::second是範圍的上界(與 upper_bound 相同)。

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

成員型別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
// map::equal_range
#include <iostream>
#include <map>

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

  mymap['a']=10;
  mymap['b']=20;
  mymap['c']=30;

  std::pair<std::map<char,int>::iterator,std::map<char,int>::iterator> ret;
  ret = mymap.equal_range('b');

  std::cout << "lower bound points to: ";
  std::cout << ret.first->first << " => " << ret.first->second << '\n';

  std::cout << "upper bound points to: ";
  std::cout << ret.second->first << " => " << ret.second->second << '\n';

  return 0;
}

lower bound points to: 'b' => 20
upper bound points to: 'c' => 30


複雜度

size 的對數複雜度。

迭代器有效性

沒有變化。

資料競爭

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

異常安全

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

另見