equal_range

提供:cppreference.com
移動: 案内, 検索

文法:

    #include <algorithm>
    pair<forward_iterator,forward_iterator> equal_range( forward_iterator first, forward_iterator last, const T& val );
    pair<forward_iterator,forward_iterator> equal_range( forward_iterator first, forward_iterator last, const T& val, CompFn comp );

equal_range()関数はfirstからlastまでの要素のうち、valと等しい範囲を返却します。 この関数は、firstからlastまでの要素が、もし指定されていればcompが、そうでなければ<オペレータによって整列されているものと仮定します。

equal_range()は lower_bound upper_boundの組み合わせとして考えることができます。 返却するイテレータの組の1つ目はlower_bound()が返却するもので、2つめはupper_bound()が返却するものだからです。

目次

[編集] 引数

todo

[編集] 返値

todo

[編集]

例として、以下のコードは整列された整数ベクタの順列が維持されるように、 8を挿入しうるすべての場所を調べるためにequal_range()を用いています。

   vector<int> nums;
   nums.push_back( -242 );
   nums.push_back( -1 );
   nums.push_back( 0 );
   nums.push_back( 5 );
   nums.push_back( 8 );
   nums.push_back( 8 );
   nums.push_back( 11 );
 
   pair<vector<int>::iterator, vector<int>::iterator> result;
   int new_val = 8;
 
   result = equal_range( nums.begin(), nums.end(), new_val );
 
   cout << "The first place that " << new_val << " could be inserted is before "
        << *result.first << ", and the last place that it could be inserted is before "
        << *result.second << endl;

上記のコードは以下の出力を生成します:

   The first place that 8 could be inserted is before 8,
   and the last place that it could be inserted is before 11

[編集] 計算量

todo

[編集] 関連トピック

binary_search, lower_bound, upper_bound

個人用ツール
名前空間
変種
操作
案内
ツールボックス
他の言語