std::partial_sort_copy
定義於標頭檔案 <algorithm> |
||
template< class InputIt, class RandomIt > RandomIt partial_sort_copy( InputIt first, InputIt last, |
(1) | (C++20 起為 constexpr) |
template< class ExecutionPolicy, class ForwardIt, class RandomIt > |
(2) | (C++17 起) |
template< class InputIt, class RandomIt, class Compare > RandomIt partial_sort_copy( InputIt first, InputIt last, |
(3) | (C++20 起為 constexpr) |
template< class ExecutionPolicy, class ForwardIt, class RandomIt, class Compare > |
(4) | (C++17 起) |
以升序對範圍 [
first,
last)
中的部分元素進行排序,並將結果儲存在範圍 [
d_first,
d_last)
中。
最多 d_last - d_first 個元素被排序並放置到範圍 [
d_first,
d_first + n)
中。n 是要排序的元素數量(std::min(std::distance(first, last), d_last - d_first))。不保證相等元素的順序。
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 不可寫入到 d_first,則程式非良構。
如果滿足以下任何條件,則行為是未定義的:
|
(C++11 前) |
|
(C++11 起) |
目錄 |
[編輯] 引數
first, last | - | 定義待排序元素 範圍 的迭代器對 |
d_first, d_last | - | 定義排序資料將要被賦值到的元素範圍的迭代器對 |
policy | - | 要使用的 執行策略 |
comp | - | 比較函式物件(即滿足比較 (Compare) 要求的物件),若第一個引數小於(即在第二個引數之前排序)第二個引數,則返回 true。 比較函式的簽名應等效於以下內容 bool cmp(const Type1& a, const Type2& b); 儘管簽名無需具有 const&,但函式必須不修改傳遞給它的物件,並且必須能夠接受 |
型別要求 | ||
-InputIt 必須滿足 LegacyInputIterator 的要求。 | ||
-ForwardIt 必須滿足 LegacyForwardIterator 的要求。 | ||
-RandomIt 必須滿足舊式隨機訪問迭代器 (LegacyRandomAccessIterator) 的要求。 | ||
-Compare 必須滿足 Compare 的要求。 |
[編輯] 返回值
指向定義排序範圍上邊界的元素的迭代器,即 d_first + std::min(std::distance(first, last), d_last - d_first)。
[編輯] 複雜度
給定 N 為 std::distance(first, last),D 為 d_last - d_first
[編輯] 異常
帶有模板引數 ExecutionPolicy
的過載按如下方式報告錯誤
- 若作為演算法一部分呼叫的函式執行丟擲異常,且
ExecutionPolicy
為標準策略之一,則呼叫 std::terminate。對於任何其他ExecutionPolicy
,行為由實現定義。 - 如果演算法未能分配記憶體,則丟擲 std::bad_alloc。
[編輯] 可能的實現
[編輯] 示例
以下程式碼對一個整數向量進行排序,並將其複製到較小的和較大的向量中。
#include <algorithm> #include <functional> #include <iostream> #include <string_view> #include <type_traits> #include <vector> void println(std::string_view rem, const auto& v) { std::cout << rem; if constexpr (std::is_scalar_v<std::decay_t<decltype(v)>>) std::cout << v; else for (int e : v) std::cout << e << ' '; std::cout << '\n'; } int main() { const auto v0 = {4, 2, 5, 1, 3}; std::vector<int> v1{10, 11, 12}; std::vector<int> v2{10, 11, 12, 13, 14, 15, 16}; std::vector<int>::iterator it; it = std::partial_sort_copy(v0.begin(), v0.end(), v1.begin(), v1.end()); println("Writing to the smaller vector in ascending order gives: ", v1); if (it == v1.end()) println("The return value is the end iterator", ' '); it = std::partial_sort_copy(v0.begin(), v0.end(), v2.begin(), v2.end(), std::greater<int>()); println("Writing to the larger vector in descending order gives: ", v2); println("The return value is the iterator to ", *it); }
輸出
Writing to the smaller vector in ascending order gives: 1 2 3 The return value is the end iterator Writing to the larger vector in descending order gives: 5 4 3 2 1 15 16 The return value is the iterator to 15
[編輯] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
P0896R4 | C++98 | *first 不需要可寫入 d_first | 若不可寫入,則程式非良構 |
[編輯] 參閱
對一個範圍的前 N 個元素進行排序 (函式模板) | |
將一個範圍按升序排序 (函式模板) | |
對一個範圍的元素進行排序,同時保留相等元素之間的順序 (函式模板) | |
(C++20) |
複製並部分排序一個範圍的元素 (演算法函式物件) |