次の方法で共有


set::equal_range

指定したキーにキーでのキーの最初の要素に以上返しますキーの最初の要素への反復子をそれぞれのペア。

pair <const_iterator, const_iterator> equal_range ( 
   const Key& _Key 
) const; 
pair <iterator, iterator> equal_range ( 
   const Key& _Key 
);

パラメーター

  • _Key
    検索対象のセットの要素の並べ替えキーと比較される引数のキー。

戻り値

1 番目のキーと 2 番目の lower_bound の反復子のペアはキーの upper_bound です。

pr ペアの最初の反復子にアクセスするには、メンバー関数を使用します prが返されました。最初、および下限の反復子を逆参照するには、* (pr。最初)。 pr ペアの 2 番目の反復子にアクセスするには、メンバー関数を使用します prが返されました。第 2、上限の反復子を逆参照するには、* (pr。第 2)。

使用例

// set_equal_range.cpp
// compile with: /EHsc
#include <set>
#include <iostream>

int main( )
{
   using namespace std;
   typedef set<int, less< int > > IntSet;
   IntSet s1;
   set <int, less< int > > :: const_iterator s1_RcIter;
   
   s1.insert( 10 );
   s1.insert( 20 );
   s1.insert( 30 );

   pair <IntSet::const_iterator, IntSet::const_iterator> p1, p2;
   p1 = s1.equal_range( 20 );

   cout << "The upper bound of the element with "
        << "a key of 20 in the set s1 is: "
        << *(p1.second) << "." << endl;

   cout << "The lower bound of the element with "
        << "a key of 20 in the set s1 is: "
        << *(p1.first) << "." << endl;

   // Compare the upper_bound called directly 
   s1_RcIter = s1.upper_bound( 20 );
   cout << "A direct call of upper_bound( 20 ) gives "
        << *s1_RcIter << "," << endl
        << "matching the 2nd element of the pair"
        << " returned by equal_range( 20 )." << endl;

   p2 = s1.equal_range( 40 );

   // If no match is found for the key,
   // both elements of the pair return end( )
   if ( ( p2.first == s1.end( ) ) && ( p2.second == s1.end( ) ) )
      cout << "The set s1 doesn't have an element "
           << "with a key less than 40." << endl;
   else
      cout << "The element of set s1 with a key >= 40 is: "
           << *(p1.first) << "." << endl;
}
  

必要条件

ヘッダー: <set>

名前空間: std

参照

関連項目

set クラス

set::lower_bound、set::upper_bound、および set::equal_range

標準テンプレート ライブラリ