The Wayback Machine - https://web.archive.org/web/20180411163129/http://ja.cppreference.com:80/w/cpp/algorithm/equal_range
名前空間
変種
操作

std::equal_range

提供: cppreference.com
< cpp‎ | algorithm

 
 
アルゴリズムライブラリ
実行ポリシー (C++17)
非変更シーケンス操作
(C++11)(C++11)(C++11)
(C++17)
変更シーケンス操作
未初期化記憶域の操作
分割操作
ソート操作
バイナリサーチ操作
equal_range
集合操作 (ソート済み範囲に対する)
ヒープ操作
(C++11)
最小/最大演算
(C++11)
(C++17)
順列
数値演算
C のライブラリ
 
ヘッダ <algorithm> で定義
template<class ForwardIt, class T >

std::pair<ForwardIt,ForwardIt>
    equal_range( ForwardIt first, ForwardIt last,

                 const T& value );
(1)
template<class ForwardIt, class T, class Compare >

std::pair<ForwardIt,ForwardIt>
    equal_range( ForwardIt first, ForwardIt last,

                 const T& value, Compare comp );
(2)
ソート範囲value[first, last)に等しいすべての要素を含む範囲を返します。範囲は2つのイテレータ、valueより小さくない'で、もう一つは、最初の要素大きいvalue以上を指す最初の要素を指す1で定義されています。 lower_bound()と.. - 最初の反復子は、あるいはupper_bound()、二回目で取得することができる
Original:
Returns a range containing all elements equal to value in the sorted range [first, last). The range is defined by two iterators, one pointing to the first element that is not less than value and another pointing to the first element greater than value. The first iterator may be alternatively obtained with lower_bound(), the second - with upper_bound().
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
最初のバージョンは、要素を比較するoperator<使用して、2番目のバージョンは、指定された比較関数compを使用しています.
Original:
The first version uses operator< to compare the elements, the second version uses the given comparison function comp.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

目次

[編集]パラメータ

first, last -
検討する要素の範囲
Original:
the range of elements to examine
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
value -
に要素を比較する値
Original:
value to compare the elements to
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
comp - 比較関数. 最初の値が二つ目の値より小さい 場合、 ​trueを返します.

比較関数のシグネチャは以下と同等でなければなりません.

 bool cmp(const Type1 &a, const Type2 &b);

シグネチャはconstを含まなくても構いませんが, 比較関数は渡されたオブジェクトを変更してはなりません.
Type1 は、 T 型のオブジェクトから暗黙に変換可能なものでなければなりません。 型 Type2 は、 ForwardIt 型のオブジェクトの逆参照から暗黙に変換可能なものでなければなりません。 ​

型の要件
-
ForwardItForwardIterator の要件を満たさなければなりません。

[編集]値を返します

std::pair希望範囲を定義する反復子のペア、ではないvalue未満と最初の要素を指す第2の第1の要素を指す第一を含むvalueより大きい.
Original:
std::pair containing a pair of iterators defining the wanted range, the first pointing to the first element that is not less than value and the second pointing to the first element greater than value.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
どの要素value下回らないが存在しない場合は、lastは、最初の要素として返されます。同様に要素がない場合は大きいよりvaluelast番目の要素として返されます
Original:
If there are no elements not less than value, last is returned as the first element. Similarly if there are no elements greater than value, last is returned as the second element
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[編集]複雑性

firstlastとの間の距離の対数
Original:
Logarithmic in the distance between first and last
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[編集]可能な実装

1つめのバージョン
template<class ForwardIt, class T std::pair<ForwardIt,ForwardIt> equal_range(ForwardIt first, ForwardIt last, const T& value){returnstd::make_pair(std::lower_bound(first, last, value), std::upper_bound(first, last, value));}
2つめのバージョン
template<class ForwardIt, class T, class Compare>std::pair<ForwardIt,ForwardIt> equal_range(ForwardIt first, ForwardIt last, const T& value, Compare comp);{returnstd::make_pair(std::lower_bound(first, last, value, comp), std::upper_bound(first, last, value, comp));}

[編集]

#include <algorithm>#include <vector>#include <iostream>   struct S {int number;char name;   S (int number, char name ): number ( number ), name ( name ){}   // only the number is relevant with this comparisonbool operator<(const S& s )const{return number < s.number;}};     int main(){std::vector<S> vec ={{1,'A'}, {2,'B'}, {2,'C'}, {2,'D'}, {3,'F'}, {4,'G'}};   S value (2, '?');   auto p = std::equal_range(vec.begin(),vec.end(),value);   for(auto i = p.first; i != p.second;++i )std::cout<< i->name <<' ';}

出力:

B C D

[編集]参照

指定された値より小さくない最初の要素を指すイテレータを返します
(関数テンプレート)[edit]
指定された値より大きい最初の要素へのイテレータを返します
(関数テンプレート)[edit]
指定範囲に要素が存在するかどうか調べます
(関数テンプレート)[edit]
close