public member function
<set>

std::set::equal_range

pair<iterator,iterator> equal_range (const value_type& val) const;
pair<const_iterator,const_iterator> equal_range (const value_type& val) const;pair<iterator,iterator>             equal_range (const value_type& val);
獲取相等元素的範圍
返回一個包含容器中所有等同於 val 的元素的範圍的邊界。

由於 set 容器中的所有元素都是唯一的,因此返回的範圍最多隻包含一個元素。

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

set 的兩個元素被認為是等效的,如果容器的 比較物件 返回false自反地(即,無論元素以何種順序作為引數傳遞)。

引數

val
要搜尋的值。
成員型別value_type是容器中元素的型別,在 set 中定義為其第一個模板引數的別名(T).

返回值

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

成員型別iteratorconst_iterator是指向元素的 雙向迭代器 型別。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// set::equal_elements
#include <iostream>
#include <set>

int main ()
{
  std::set<int> myset;

  for (int i=1; i<=5; i++) myset.insert(i*10);   // myset: 10 20 30 40 50

  std::pair<std::set<int>::const_iterator,std::set<int>::const_iterator> ret;
  ret = myset.equal_range(30);

  std::cout << "the lower bound points to: " << *ret.first << '\n';
  std::cout << "the upper bound points to: " << *ret.second << '\n';

  return 0;
}

the lower bound points to: 30
the upper bound points to: 40


複雜度

size 的對數複雜度。

迭代器有效性

沒有變化。

資料競爭

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

異常安全

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

另見