名稱空間
變體
操作

std::ranges::partial_sort_copy, std::ranges::partial_sort_copy_result

來自 cppreference.com
< cpp‎ | 演算法‎ | 範圍
 
 
演算法庫
有約束演算法與針對範圍的演算法 (C++20)
有約束的演算法,例如 ranges::copyranges::sort 等……
執行策略 (C++17)
排序及相關操作
劃分操作
排序操作
二分搜尋操作
(於已劃分範圍上)
集合操作(於已排序範圍上)
歸併操作(於已排序範圍上)
堆操作
最小/最大值操作
(C++11)
(C++17)
字典序比較操作
排列操作
C 庫
數值操作
未初始化記憶體上的操作
 
受約束演算法
此選單中的所有名稱均屬於名稱空間 std::ranges
非修改序列操作
修改序列操作
劃分操作
排序操作
       
partial_sort_copy

二分查詢操作(在已排序的範圍內)
       
       
集合操作(於已排序範圍上)
堆操作
最小/最大值操作
       
       
排列操作
摺疊操作
數值操作
(C++23)            
對未初始化儲存的操作
返回型別
 
定義於標頭檔案 <algorithm>
呼叫簽名 (Call signature)
template< std::input_iterator I1, std::sentinel_for<I1> S1,

          std::random_access_iterator I2, std::sentinel_for<I2> S2,
          class Comp = ranges::less, class Proj1 = std::identity,
          class Proj2 = std::identity >
requires std::indirectly_copyable<I1, I2> &&
         std::sortable<I2, Comp, Proj2> &&
         std::indirect_strict_weak_order<Comp, std::projected<I1, Proj1>,
             std::projected<I2, Proj2>>
constexpr partial_sort_copy_result<I1, I2>
    partial_sort_copy( I1 first, S1 last, I2 result_first, S2 result_last,

                       Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {} );
(1) (C++20 起)
template< ranges::input_range R1, ranges::random_access_range R2,

          class Comp = ranges::less, class Proj1 = std::identity,
          class Proj2 = std::identity >
requires std::indirectly_copyable<ranges::iterator_t<R1>, ranges::iterator_t<R2>> &&
         std::sortable<ranges::iterator_t<R2>, Comp, Proj2> &&
         std::indirect_strict_weak_order<Comp, std::projected<ranges::iterator_t<R1>,
             Proj1>, std::projected<ranges::iterator_t<R2>, Proj2>>
constexpr partial_sort_copy_result<ranges::borrowed_iterator_t<R1>,
                                   ranges::borrowed_iterator_t<R2>>
    partial_sort_copy( R1&& r, R2&& result_r,

                       Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {} );
(2) (C++20 起)
輔助型別
template< class I, class O >
using partial_sort_copy_result = ranges::in_out_result<I, O>;
(3) (C++20 起)

將源範圍 [firstlast) 的前 N 個元素複製到目標範圍 [result_firstresult_first + N),如同它們已根據 compproj1 進行了部分排序。其中 N = min(L₁, L₂)L₁ 等於 ranges::distance(first, last)L₂ 等於 ranges::distance(result_first, result_last)

不保證相等元素的順序得到保留。

1) 使用函式物件 proj1 投影源範圍元素,並使用函式物件 proj2 投影目標元素。
2)(1),但使用 r 作為源範圍,result_r 作為目標範圍,如同使用 ranges::begin(r) 作為 firstranges::end(r) 作為 lastranges::begin(result_r) 作為 result_first,以及 ranges::end(result_r) 作為 result_last

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

目錄

[編輯] 引數

first, last - 定義要複製的源元素範圍的迭代器-哨兵對
r - 要複製的源範圍
result_first, result_last - 定義目標元素範圍的迭代器-哨兵對
result_r - 目標範圍
comp - 應用於投影元素的比較
proj1 - 應用於源範圍元素的投影
proj2 - 應用於目標範圍元素的投影

[編輯] 返回值

一個等於 {last, result_first + N} 的物件。

[編輯] 複雜度

至多 L₁•log(N) 次比較和 2•L₁•log(N) 次投影。

[編輯] 可能的實現

struct partial_sort_copy_fn
{
    template<std::input_iterator I1, std::sentinel_for<I1> S1,
             std::random_access_iterator I2, std::sentinel_for<I2> S2,
             class Comp = ranges::less, class Proj1 = std::identity,
             class Proj2 = std::identity>
    requires std::indirectly_copyable<I1, I2> && std::sortable<I2, Comp, Proj2> &&
             std::indirect_strict_weak_order<Comp, std::projected<I1, Proj1>,
             std::projected<I2, Proj2>>
    constexpr ranges::partial_sort_copy_result<I1, I2>
        operator()(I1 first, S1 last, I2 result_first, S2 result_last,
                   Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) const
    {
        if (result_first == result_last)
            return {std::move(ranges::next(std::move(first), std::move(last))),
                    std::move(result_first)};
 
        auto out_last{result_first};
        // copy first N elements
        for (; !(first == last or out_last == result_last); ++out_last, ++first)
            *out_last = *first;
 
        // convert N copied elements into a max-heap
        ranges::make_heap(result_first, out_last, comp, proj2);
 
        // process the rest of the input range (if any), preserving the heap property
        for (; first != last; ++first)
        {
            if (std::invoke(comp, std::invoke(proj1, *first),
                                  std::invoke(proj2, *result_first)))
            {
                // pop out the biggest item and push in a newly found smaller one
                ranges::pop_heap(result_first, out_last, comp, proj2);
                *(out_last - 1) = *first;
                ranges::push_heap(result_first, out_last, comp, proj2);
            }
        }
 
        // first N elements in the output range is still
        // a heap - convert it into a sorted range
        ranges::sort_heap(result_first, out_last, comp, proj2);
 
        return {std::move(first), std::move(out_last)};
    }
 
    template<ranges::input_range R1, ranges::random_access_range R2,
             class Comp = ranges::less, class Proj1 = std::identity,
             class Proj2 = std::identity>
    requires std::indirectly_copyable<ranges::iterator_t<R1>, ranges::iterator_t<R2>> &&
             std::sortable<ranges::iterator_t<R2>, Comp, Proj2> &&
             std::indirect_strict_weak_order<Comp, std::projected<ranges::iterator_t<R1>,
             Proj1>, std::projected<ranges::iterator_t<R2>, Proj2>>
    constexpr ranges::partial_sort_copy_result<ranges::borrowed_iterator_t<R1>,
              ranges::borrowed_iterator_t<R2>>
        operator()(R1&& r, R2&& result_r, Comp comp = {},
                   Proj1 proj1 = {}, Proj2 proj2 = {}) const
    {
        return (*this)(ranges::begin(r), ranges::end(r),
                       ranges::begin(result_r), ranges::end(result_r),
                       std::move(comp), std::move(proj1), std::move(proj2));
    }
};
 
inline constexpr partial_sort_copy_fn partial_sort_copy {};

[編輯] 示例

#include <algorithm>
#include <forward_list>
#include <functional>
#include <iostream>
#include <ranges>
#include <string_view>
#include <vector>
 
void print(std::string_view rem, std::ranges::input_range auto const& v)
{
    for (std::cout << rem; const auto& e : v)
        std::cout << e << ' ';
    std::cout << '\n';
}
 
int main()
{
    const std::forward_list source{4, 2, 5, 1, 3};
 
    print("Write to the smaller vector in ascending order: ", "");
 
    std::vector dest1{10, 11, 12};
    print("const source list: ", source);
    print("destination range: ", dest1);
    std::ranges::partial_sort_copy(source, dest1);
    print("partial_sort_copy: ", dest1);
 
    print("Write to the larger vector in descending order:", "");
 
    std::vector dest2{10, 11, 12, 13, 14, 15, 16};
    print("const source list: ", source);
    print("destination range: ", dest2);
    std::ranges::partial_sort_copy(source, dest2, std::greater{});
    print("partial_sort_copy: ", dest2);
}

輸出

Write to the smaller vector in ascending order:
const source list: 4 2 5 1 3
destination range: 10 11 12
partial_sort_copy: 1 2 3
Write to the larger vector in descending order:
const source list: 4 2 5 1 3
destination range: 10 11 12 13 14 15 16
partial_sort_copy: 5 4 3 2 1 15 16

[編輯] 參閱

對一個範圍的前 N 個元素進行排序
(演算法函式物件)[編輯]
將一個範圍按升序排序
(演算法函式物件)[編輯]
對一個範圍的元素進行排序,同時保留相等元素之間的順序
(演算法函式物件)[編輯]
將一個最大堆轉換成一個按升序排序的元素範圍
(演算法函式物件)[編輯]
從一個元素範圍建立一個最大堆
(演算法函式物件)[編輯]
向一個最大堆新增一個元素
(演算法函式物件)[編輯]
從一個最大堆中移除最大的元素
(演算法函式物件)[編輯]
複製並部分排序一個範圍的元素
(函式模板) [編輯]