共用方式為


set::lower_bound, set::upper_bound, 和 set::equal_range

說明如何使用 set::lower_bound, set::upper_bound,和 set::equal_range Visual C++ 標準樣板程式庫 (STL) 函式。

template<class _K, class _Pr, class _A>
   class set 
   {
      public:
      // Function 1:
      const_iterator lower_bound(const _K& _Kv) const;
      // Function 2:
      const_iterator upper_bound(const _K& _Kv) const;
      // Function 3:
      _Paircc equal_range(const _K& _Kv) const;
   }

備註

注意事項注意事項

在原型中的類別/參數名稱不相符的標頭檔中的版本。某些已修改以提高可讀性。

lower_bound函式會傳回 iterator 中含有不符合傳遞至值的索引鍵的受控制序列的最早的項目lower_bound函式。 upper_bound函式中有索引鍵相符的值傳遞給受控制序列的最早的項目會傳回 iterator upper_bound函式。 如果沒有這類項目就會存在,則函數會傳回結束。 在這兩種情況下,函式set::key_comp(key, x) 用來判斷索引鍵比對。 equal_range函式會傳回一個配對的值,其中。First的結果lower_bound函式和.second 是因為upper_bound函式。

範例

// SetBoundRange.cpp
// compile with: /EHsc
//
//      Illustrates how to use the lower_bound function to get an
//      iterator to the earliest element in the controlled sequence
//      that has a key that does not match the value passed to the
//      function. It also illustrates how to use the upper_bound
//      function to get an iterator to the earliest element in the
//      controlled sequence that has a key that matches the value
//      passed to the function. The last thing it illustrates is
//      how to use the equal_range function to get a pair value that
//      contains the lower_bound and upper_bound results of the key.
//
// Functions:
//
//    lower_bound  Returns an iterator whose value does not match the
//                 key passed to the function, or end() if no such
//                 element exists.
//    upper_bound  Returns an iterator whose value matches the key
//                 passed to the function, or end() if no such element
//                 exists.
//    equal_range  Returns a pair of (lower_bound,upper_bound).
//////////////////////////////////////////////////////////////////////

#pragma warning(disable:4786)
#include <set>
#include <iostream>

using namespace std ;

typedef set<int> SET_INT;

int main() {
  SET_INT s1;
  SET_INT::iterator i;
  cout << "s1.insert(5)" << endl;
  s1.insert(5);
  cout << "s1.insert(10)" << endl;
  s1.insert(10);
  cout << "s1.insert(15)" << endl;
  s1.insert(15);
  cout << "s1.insert(20)" << endl;
  s1.insert(20);
  cout << "s1.insert(25)" << endl;
  s1.insert(25);

  cout << "s1 -- starting at s1.lower_bound(12)" << endl;
// prints: 15,20,25
  for (i=s1.lower_bound(12);i!=s1.end();i++)
     cout << "s1 has " << *i << " in its set." << endl;

  cout << "s1 -- starting at s1.lower_bound(15)" << endl;
// prints: 15,20,25
  for (i=s1.lower_bound(15);i!=s1.end();i++)
     cout << "s1 has " << *i << " in its set." << endl;

  cout << "s1 -- starting at s1.upper_bound(12)" << endl;
// prints: 15,20,25
  for (i=s1.upper_bound(12);i!=s1.end();i++)
     cout << "s1 has " << *i << " in its set." << endl;

  cout << "s1 -- starting at s1.upper_bound(15)" << endl;
// prints: 20,25
  for (i=s1.upper_bound(15);i!=s1.end();i++)
     cout << "s1 has " << *i << " in its set." << endl;

  cout << "s1 -- s1.equal_range(12)" << endl;
// does not print anything
  for (i=s1.equal_range(12).first;i!=s1.equal_range(12).second;i++)
     cout << "s1 has " << *i << " in its set." << endl;

  cout << "s1 -- s1.equal_range(15)" << endl;
// prints: 15
  for (i=s1.equal_range(15).first;i!=s1.equal_range(15).second;i++)
     cout << "s1 has " << *i << " in its set." << endl;
}

Output

s1.insert(5)
s1.insert(10)
s1.insert(15)
s1.insert(20)
s1.insert(25)
s1 -- starting at s1.lower_bound(12)
s1 has 15 in its set.
s1 has 20 in its set.
s1 has 25 in its set.
s1 -- starting at s1.lower_bound(15)
s1 has 15 in its set.
s1 has 20 in its set.
s1 has 25 in its set.
s1 -- starting at s1.upper_bound(12)
s1 has 15 in its set.
s1 has 20 in its set.
s1 has 25 in its set.
s1 -- starting at s1.upper_bound(15)
s1 has 20 in its set.
s1 has 25 in its set.
s1 -- s1.equal_range(12)
s1 -- s1.equal_range(15)
s1 has 15 in its set.

需求

標頭: <set>

請參閱

概念

標準樣板程式庫範例