C++ 集合庫 - equal_range 函式



描述

它返回一個範圍的邊界,該範圍包含容器中所有與 val 等效的元素。

宣告

以下是 std::set::equal_range 在不同 C++ 版本中的工作方式。

C++98

iterator upper_bound (const value_type& val) const;

C++11

  iterator upper_bound (const value_type& val);
const_iterator upper_bound (const value_type& val) const;

返回值

它返回一個範圍的邊界,該範圍包含容器中所有與 val 等效的元素。

異常

如果丟擲異常,容器不會發生任何更改。

時間複雜度

時間複雜度取決於對數。

示例

以下示例演示了 std::set::equal_range 的用法。

#include <iostream>
#include <set>

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

   for (int i = 1; i <= 5; i++) myset.insert(i*10);

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

   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: 10
the upper bound points to: 20
set.htm
廣告