名前空間
変種
操作

「cpp/algorithm/nth element」の版間の差分

提供: cppreference.com
< cpp‎ | algorithm
(Translated from the English version using Google Translate)
 
(1版:Translate from the English version)

2012年10月30日 (火) 15:02時点における版

 
 
アルゴリズムライブラリ
制約付きアルゴリズムと範囲に対するアルゴリズム (C++20)
コンセプトとユーティリティ: std::sortable, std::projected, ...
制約付きアルゴリズム: std::ranges::copy, std::ranges::sort, ...
実行ポリシー (C++17)
非変更シーケンス操作
(C++11)(C++11)(C++11)
(C++17)
変更シーケンス操作
未初期化記憶域の操作
分割操作
ソート操作
(C++11)
nth_element

二分探索操作
集合操作 (ソート済み範囲用)
ヒープ操作
(C++11)
最小/最大演算
(C++11)
(C++17)

順列
数値演算
C のライブラリ
 
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.

目次

パラメータ

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 - comparison function which returns ​true if the first argument is less than the second.

The signature of the comparison function should be equivalent to the following:

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

The signature does not need to have const &, but the function must not modify the objects passed to it.
Type1 および Type2 は、どちらも RandomIt 型のオブジェクトの逆参照から暗黙に変換可能なものでなければなりません。 ​

Type requirements
-
RandomIt must meet the requirements of ValueSwappable and LegacyRandomAccessIterator.
-
The type of dereferenced RandomIt must meet the requirements of MoveAssignable and MoveConstructible.

値を返します

(なし)
Original:
(none)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

複雑

平均で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

も参照してください

テンプレート:cpp/algorithm/dcl list partial sort copyテンプレート:cpp/algorithm/dcl list stable sortテンプレート:cpp/algorithm/dcl list sort