std::equal_range
提供: cppreference.com
![]() | このページは、Google 翻訳を使って英語版から機械翻訳されました。 翻訳には誤りや奇妙な言い回しがあるかもしれません。文章の上にポインタをおくと、元の文章が見れます。誤りを修正して翻訳を改善する手助けをしてください。翻訳についての説明は、ここをクリックしてください。 |
ヘッダ <algorithm> で定義 | ||
template<class ForwardIt, class T > std::pair<ForwardIt,ForwardIt> | (1) | |
template<class ForwardIt, class T, class Compare > std::pair<ForwardIt,ForwardIt> | (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.
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.
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); シグネチャは |
型の要件 | ||
-ForwardIt は ForwardIterator の要件を満たさなければなりません。 |
[編集]値を返します
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.
You can help to correct and verify the translation. Click here for instructions.
どの要素
value
下回らないが存在しない場合は、last
は、最初の要素として返されます。同様に要素がない場合は大きいよりvalue
、last
番目の要素として返されます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 elementThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[編集]複雑性
first
last
との間の距離の対数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.
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));} |
[編集]例
Run this code
#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
[編集]参照
指定された値より小さくない最初の要素を指すイテレータを返します (関数テンプレート) | |
指定された値より大きい最初の要素へのイテレータを返します (関数テンプレート) | |
指定範囲に要素が存在するかどうか調べます (関数テンプレート) |