std::nth_element
提供: cppreference.com
![]() | このページは、Google 翻訳を使って英語版から機械翻訳されました。 翻訳には誤りや奇妙な言い回しがあるかもしれません。文章の上にポインタをおくと、元の文章が見れます。誤りを修正して翻訳を改善する手助けをしてください。翻訳についての説明は、ここをクリックしてください。 |
Defined in header <algorithm> | ||
template<class RandomIt > void nth_element( RandomIt first, RandomIt nth, RandomIt last ); | (1) | |
template<class RandomIt, class Compare > void nth_element( RandomIt first, RandomIt nth, RandomIt last, Compare comp ); | (2) | |
部分的にソートされるように昇順で範囲
[first, last)
範囲内のすべての要素[first, nth)
アール範囲[nth, last)
に比べて少ない。最初のバージョンは、要素を比較するoperator<使用して、2番目のバージョンは、指定された比較関数comp
を使用しています。 nth
位置に配置要素は、正確に範囲が完全にソートされていた場合、この位置に現れることになる要素です.Original:
Partially sorts the range
[first, last)
in ascending order so that all elements in the range [first, nth)
are less than those in the range [nth, last)
. The first version uses operator< to compare the elements, the second version uses the given comparison function comp
. The element placed in the nth
position is exactly the element that would occur in this position if the range was fully sorted.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: random access iterators defining the range sort The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
nth | - | ソート·パーティション·ポイントを定義するランダムアクセス反復子を返します Original: random access iterator defining the sort partition point 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); シグネチャは |
型の要件 | ||
-RandomIt は ValueSwappable and RandomAccessIterator 。 | ||
-The type of dereferenced RandomIt must meet the requirements of MoveAssignable and MoveConstructible . |
[編集]値を返します
(なし)
[編集]複雑性
平均でstd::distance(first, last)におけるリニア.
Original:
Linear in std::distance(first, last) on average.
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.
[編集]例
このコードを実行します
#include <iostream>#include <vector>#include <algorithm>#include <functional> int main(){std::vector<int> v{5, 6, 4, 3, 2, 6, 7, 9, 3}; std::nth_element(v.begin(), v.begin()+ v.size()/2, v.end());std::cout<<"The median is "<< v[v.size()/2]<<'\n'; std::nth_element(v.begin(), v.begin()+1, v.end(), std::greater<int>());std::cout<<"The second largest element is "<< v[1]<<'\n';}
出力:
The median is 5 The second largest element is 7
[編集]参照
コピーと部分的にソートする要素の範囲 Original: copies and partially sorts a range of elements The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (関数テンプレート) | |
ソートされた範囲の要素を等しい要素間の順序を維持しながら Original: sorts a range of elements while preserving order between equal elements The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (関数テンプレート) | |
昇順にソートする範囲 Original: sorts a range into ascending order The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (関数テンプレート) |