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

std::nth_element

提供: cppreference.com
< cpp‎ | algorithm

 
 
アルゴリズムライブラリ
実行ポリシー (C++17)
非変更シーケンス操作
(C++11)(C++11)(C++11)
(C++17)
変更シーケンス操作
未初期化記憶域の操作
分割操作
ソート操作
nth_element

バイナリサーチ操作
集合操作 (ソート済み範囲に対する)
ヒープ操作
(C++11)
最小/最大演算
(C++11)
(C++17)
順列
数値演算
C のライブラリ
 
ヘッダ <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.

目次

[編集]パラメータ

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);

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

型の要件
-
RandomItValueSwappable および 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.

[編集]

#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

[編集]参照

指定範囲の最初の N 個の要素がソートされたコピーを作成します
(関数テンプレート)[edit]
等しい要素間の順序を維持しながら指定範囲の要素をソートします
(関数テンプレート)[edit]
指定範囲を昇順にソートします
(関数テンプレート)[edit]
close