名稱空間
變體
操作

std::ranges::nth_element

來自 cppreference.com
< cpp‎ | 演算法‎ | 範圍
 
 
演算法庫
有約束演算法與針對範圍的演算法 (C++20)
有約束的演算法,例如 ranges::copyranges::sort 等……
執行策略 (C++17)
排序及相關操作
劃分操作
排序操作
二分搜尋操作
(於已劃分範圍上)
集合操作(於已排序範圍上)
歸併操作(於已排序範圍上)
堆操作
最小/最大值操作
(C++11)
(C++17)
字典序比較操作
排列操作
C 庫
數值操作
未初始化記憶體上的操作
 
受約束演算法
此選單中的所有名稱均屬於名稱空間 std::ranges
非修改序列操作
修改序列操作
劃分操作
排序操作
       
     
nth_element
二分查詢操作(在已排序的範圍內)
       
       
集合操作(於已排序範圍上)
堆操作
最小/最大值操作
       
       
排列操作
摺疊操作
數值操作
(C++23)            
對未初始化儲存的操作
返回型別
 
定義於標頭檔案 <algorithm>
呼叫簽名 (Call signature)
template< std::random_access_iterator I, std::sentinel_for<I> S,

          class Comp = ranges::less, class Proj = std::identity >
requires std::sortable<I, Comp, Proj>
constexpr I

    nth_element( I first, I nth, S last, Comp comp = {}, Proj proj = {} );
(1) (C++20 起)
template< ranges::random_access_range R,

          class Comp = ranges::less, class Proj = std::identity >
requires std::sortable<iterator_t<R>, Comp, Proj>
constexpr ranges::borrowed_iterator_t<R>

    nth_element( R&& r, iterator_t<R> nth, Comp comp = {}, Proj proj = {} );
(2) (C++20 起)

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

  • nth 指向的元素被更改為,如果 [firstlast) 根據 compproj 排序,則該位置上出現的元素。
  • 所有在此新的 nth 元素之前的元素都 _小於或等於_ 此新的 nth 元素之後的元素。也就是說,對於範圍 [firstnth)[nthlast) 中的每個迭代器 _i_ 和 _j_,表示式 std::invoke(comp, std::invoke(proj, *j), std::invoke(proj, *i)) 評估為 false
  • 如果 nth == last,則函式無效果。
1) 元素使用給定的二元比較函式物件 comp 和投影物件 proj 進行比較。
2)(1),但使用 r 作為範圍,如同使用 ranges::begin(r) 作為 first,使用 ranges::end(r) 作為 last

本頁描述的類函式實體是 演算法函式物件(非正式地稱為 niebloids),即

目錄

[編輯] 引數

first, last - 定義要重新排列的元素的範圍的迭代器-哨兵對
r - 要重新排列的元素範圍
nth - 定義分割槽點的迭代器
comp - 用於比較投影元素的比較器
proj - 應用於元素的投影

[編輯] 返回值

1) 一個等於 last 的迭代器。
2) 如果 r 是左值或 borrowed_range 型別,則與 (1) 相同。否則返回 std::ranges::dangling

[編輯] 複雜度

平均情況下,與 ranges::distance(first, last) 呈線性關係。

[編輯] 注意

使用的演算法通常是 內省選擇(introselect),但也允許其他具有適當平均情況複雜度的 選擇演算法

[編輯] 可能的實現

另請參閱 msvc stllibstdc++ 和 libc++ 中的實現:(1) / (2)

[編輯] 示例

#include <algorithm>
#include <array>
#include <functional>
#include <iostream>
#include <ranges>
#include <string_view>
 
void print(std::string_view rem, std::ranges::input_range auto const& a)
{
    for (std::cout << rem; const auto e : a)
        std::cout << e << ' ';
    std::cout << '\n';
}
 
int main()
{
    std::array v{5, 6, 4, 3, 2, 6, 7, 9, 3};
    print("Before nth_element: ", v);
 
    std::ranges::nth_element(v, v.begin() + v.size() / 2);
    print("After nth_element:  ", v);
    std::cout << "The median is: " << v[v.size() / 2] << '\n';
 
    std::ranges::nth_element(v, v.begin() + 1, std::greater<int>());
    print("After nth_element:  ", v);
    std::cout << "The second largest element is: " << v[1] << '\n';
    std::cout << "The largest element is: " << v[0] << "\n\n";
 
    using namespace std::literals;
    std::array names
    {
        "Diva"sv, "Cornelius"sv, "Munro"sv, "Rhod"sv,
        "Zorg"sv, "Korben"sv, "Bender"sv, "Leeloo"sv,
    };
    print("Before nth_element: ", names);
    auto fifth_element{std::ranges::next(names.begin(), 4)};
    std::ranges::nth_element(names, fifth_element);
    print("After nth_element:  ", names);
    std::cout << "The 5th element is: " << *fifth_element << '\n';
}

輸出

Before nth_element: 5 6 4 3 2 6 7 9 3 
After nth_element:  2 3 3 4 5 6 6 7 9 
The median is: 5
After nth_element:  9 7 6 6 5 4 3 3 2 
The second largest element is: 7
The largest element is: 9
 
Before nth_element: Diva Cornelius Munro Rhod Zorg Korben Bender Leeloo 
After nth_element:  Diva Cornelius Bender Korben Leeloo Rhod Munro Zorg 
The 5th element is: Leeloo

[編輯] 參閱

返回一個範圍中最大的元素
(演算法函式物件)[編輯]
返回一個範圍中最小的元素
(演算法函式物件)[編輯]
將一個範圍的元素分成兩組
(演算法函式物件)[編輯]
對一個範圍的前 N 個元素進行排序
(演算法函式物件)[編輯]
部分排序給定的範圍,確保它被給定的元素劃分
(函式模板) [編輯]