名稱空間
變體
操作

std::nth_element

來自 cppreference.com
< cpp‎ | 演算法
 
 
演算法庫
有約束演算法與針對範圍的演算法 (C++20)
有約束的演算法,例如 ranges::copyranges::sort 等……
執行策略 (C++17)
排序及相關操作
劃分操作
排序操作
(C++11)
nth_element

二分搜尋操作
(於已劃分範圍上)
集合操作(於已排序範圍上)
歸併操作(於已排序範圍上)
堆操作
最小/最大值操作
(C++11)
(C++17)
字典序比較操作
排列操作
C 庫
數值操作
未初始化記憶體上的操作
 
定義於標頭檔案 <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,

                  RandomIt first, RandomIt nth, RandomIt last );
(2) (C++17 起)
template< class RandomIt, class Compare >

void nth_element( RandomIt first, RandomIt nth, RandomIt last,

                  Compare comp );
(3) (C++20 起為 constexpr)
template< class ExecutionPolicy, class RandomIt, class Compare >

void nth_element( ExecutionPolicy&& policy,
                  RandomIt first, RandomIt nth, RandomIt last,

                  Compare comp );
(4) (C++17 起)

nth_element 重新排列 [firstlast) 中的元素,使得重排後

  • nth 指向的元素變為如果 [firstlast) 排序後在該位置上出現的元素。
  • 對於 [firstnth) 中的每個迭代器 i[nthlast) 中的每個迭代器 j,滿足以下條件:
1,2) bool(*j < *i)(C++20 前)std::less{}(*j, *i)(C++20 起)false
3,4) bool(comp(*j, *i))false


1) 元素根據 operator<(C++20 前)std::less{}(C++20 起) 假定已排序
3) 元素根據 comp 假定已排序。
2,4)(1,3),但按 policy 執行。
僅當滿足所有以下條件時,這些過載才參與過載決議

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

如果滿足以下任何條件,則行為是未定義的:

(C++11 前)
(C++11 起)

目錄

[編輯] 引數

first, last - 定義用於部分排序的元素範圍的迭代器對。
nth - 定義排序分割槽點的隨機訪問迭代器。
policy - 要使用的 執行策略
comp - 比較函式物件(即滿足比較 (Compare)要求,並返回 true 如果第一個引數“小於”(即排在“之前”)第二個引數的物件)。

比較函式的簽名應等效於以下內容

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

雖然簽名不需要有 const&,但該函式不能修改傳遞給它的物件,並且必須能夠接受所有型別為 (可能是 const) Type1Type2 的值,無論其值類別如何(因此,不允許使用 Type1&,也不允許使用 Type1,除非對於 Type1,移動等同於複製(C++11 起))。
型別 Type1Type2 必須是這樣的:型別為 RandomIt 的物件可以被解引用,然後隱式轉換為這兩種型別。​

型別要求
-
RandomIt 必須滿足舊式隨機訪問迭代器 (LegacyRandomAccessIterator)的要求。
-
Compare 必須滿足 Compare 的要求。

[編輯] 複雜度

給定 Nlast - first

1) 平均進行 O(N) 次比較,使用 operator<(C++20 前)std::less{}(C++20 起)
2) 進行 O(N) 次比較,使用 operator<(C++20 前)std::less{}(C++20 起),以及 O(N·log(N)) 次交換。
3) 平均進行 O(N) 次比較器 comp 的應用。
4) 進行 O(N) 次比較器 comp 的應用,以及 O(N·log(N)) 次交換。

[編輯] 異常

帶有模板引數 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 [firstnth)[nthlast)
不再要求為有效範圍
行為未定義
如果其中任何一個無效

[編輯] 參閱

返回一個範圍中最大的元素
(函式模板) [編輯]
返回一個範圍中最小的元素
(函式模板) [編輯]
複製並部分排序一個範圍的元素
(函式模板) [編輯]
對一個範圍的元素進行排序,同時保留相等元素之間的順序
(函式模板) [編輯]
將一個範圍按升序排序
(函式模板) [編輯]
部分排序給定的範圍,確保它被給定的元素劃分
(演算法函式物件)[編輯]