std::nth_element
定義於標頭檔案 <algorithm> |
||
template< class RandomIt > void nth_element( RandomIt first, RandomIt nth, RandomIt last ); |
(1) | (C++20 起為 constexpr) |
template< class ExecutionPolicy, class RandomIt > void nth_element( ExecutionPolicy&& policy, |
(2) | (C++17 起) |
template< class RandomIt, class Compare > void nth_element( RandomIt first, RandomIt nth, RandomIt last, |
(3) | (C++20 起為 constexpr) |
template< class ExecutionPolicy, class RandomIt, class Compare > void nth_element( ExecutionPolicy&& policy, |
(4) | (C++17 起) |
nth_element
重新排列 [
first,
last)
中的元素,使得重排後
- nth 指向的元素變為如果
[
first,
last)
排序後在該位置上出現的元素。 - 對於
[
first,
nth)
中的每個迭代器 i 和[
nth,
last)
中的每個迭代器 j,滿足以下條件:
std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> 為 true。 |
(C++20 前) |
std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> 為 true。 |
(C++20 起) |
如果滿足以下任何條件,則行為是未定義的:
-
[
first,
nth)
或[
nth,
last)
不是有效範圍。
|
(C++11 前) |
|
(C++11 起) |
目錄 |
[編輯] 引數
first, last | - | 定義用於部分排序的元素範圍的迭代器對。 |
nth | - | 定義排序分割槽點的隨機訪問迭代器。 |
policy | - | 要使用的 執行策略 |
comp | - | 比較函式物件(即滿足比較 (Compare)要求,並返回 true 如果第一個引數“小於”(即排在“之前”)第二個引數的物件)。 比較函式的簽名應等效於以下內容 bool cmp(const Type1& a, const Type2& b); 雖然簽名不需要有 const&,但該函式不能修改傳遞給它的物件,並且必須能夠接受所有型別為 (可能是 const) |
型別要求 | ||
-RandomIt 必須滿足舊式隨機訪問迭代器 (LegacyRandomAccessIterator)的要求。 | ||
-Compare 必須滿足 Compare 的要求。 |
[編輯] 複雜度
給定 N 為 last - first
[編輯] 異常
帶有模板引數 ExecutionPolicy
的過載按如下方式報告錯誤
- 若作為演算法一部分呼叫的函式執行丟擲異常,且
ExecutionPolicy
為標準策略之一,則呼叫 std::terminate。對於任何其他ExecutionPolicy
,行為是實現定義的。 - 如果演算法未能分配記憶體,則丟擲 std::bad_alloc。
[編輯] 可能的實現
另請參見 libstdc++、libc++ 和 MSVC STL 中的實現。
[編輯] 注意
使用的演算法通常是 Introselect,但也允許使用其他具有合適平均複雜度選擇演算法。
[編輯] 示例
#include <algorithm> #include <cassert> #include <functional> #include <iostream> #include <numeric> #include <vector> void printVec(const std::vector<int>& vec) { std::cout << "v = {"; for (char sep[]{0, ' ', 0}; const int i : vec) std::cout << sep << i, sep[0] = ','; std::cout << "};\n"; } int main() { std::vector<int> v{5, 10, 6, 4, 3, 2, 6, 7, 9, 3}; printVec(v); auto m = v.begin() + v.size() / 2; std::nth_element(v.begin(), m, v.end()); std::cout << "\nThe median is " << v[v.size() / 2] << '\n'; // The consequence of the inequality of elements before/after the Nth one: assert(std::accumulate(v.begin(), m, 0) < std::accumulate(m, v.end(), 0)); printVec(v); // Note: comp function changed std::nth_element(v.begin(), v.begin() + 1, v.end(), std::greater{}); std::cout << "\nThe second largest element is " << v[1] << '\n'; std::cout << "The largest element is " << v[0] << '\n'; printVec(v); }
可能的輸出
v = {5, 10, 6, 4, 3, 2, 6, 7, 9, 3}; The median is 6 v = {3, 2, 3, 4, 5, 6, 10, 7, 9, 6}; The second largest element is 9 The largest element is 10 v = {10, 9, 6, 7, 6, 3, 5, 4, 3, 2};
[編輯] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 2150 | C++98 | 重排後,在 nth 之前只有一個元素 要求不大於 nth 之後的一個元素 |
已更正 要求 |
LWG 2163 | C++98 | 過載 (1) 使用 operator> 比較元素 | 改為 operator< |
P0896R4 | C++98 | [ first, nth) 和 [ nth, last) 不再要求為有效範圍 |
行為未定義 如果其中任何一個無效 |
[編輯] 參閱
返回一個範圍中最大的元素 (函式模板) | |
返回一個範圍中最小的元素 (函式模板) | |
複製並部分排序一個範圍的元素 (函式模板) | |
對一個範圍的元素進行排序,同時保留相等元素之間的順序 (函式模板) | |
將一個範圍按升序排序 (函式模板) | |
(C++20) |
部分排序給定的範圍,確保它被給定的元素劃分 (演算法函式物件) |