std::ranges::merge, std::ranges::merge_result
定義於標頭檔案 <algorithm> |
||
呼叫簽名 (Call signature) |
||
template< std::input_iterator I1, std::sentinel_for<I1> S1, std::input_iterator I2, std::sentinel_for<I2> S2, |
(1) | (C++20 起) |
template< ranges::input_range R1, ranges::input_range R2, std::weakly_incrementable O, class Comp = ranges::less, |
(2) | (C++20 起) |
輔助型別 |
||
template< class I1, class I2, class O > using merge_result = ranges::in_in_out_result<I1, I2, O>; |
(3) | (C++20 起) |
將兩個*已排序*範圍`[``[first1`, `last1`)` 和 `[``first2`, `last2`)` 合併到一個從 result 開始的*已排序*範圍中。
如果對於指向序列的任何迭代器 `it` 和任何非負整數 `n`,使得 `it + n` 是指向序列元素的有效迭代器,std::invoke(comp, std::invoke(proj2, *(it + n)), std::invoke(proj1, *it))) 評估為 false,則稱序列相對於比較器 comp 是*已排序*的。
如果目標範圍與任一輸入範圍重疊,則行為未定義(輸入範圍可以相互重疊)。
此合併函式是*穩定的*,這意味著對於原始兩個範圍中的等效元素,第一個範圍中的元素(保留其原始順序)優先於第二個範圍中的元素(保留其原始順序)。
本頁描述的類函式實體是 演算法函式物件(非正式地稱為 niebloids),即
目錄 |
[編輯] 引數
first1, last1 | - | 定義要合併的第一個輸入已排序範圍元素的迭代器-哨兵對 |
first2, last2 | - | 定義要合併的第二個輸入已排序範圍元素的迭代器-哨兵對 |
result | - | 輸出範圍的起始 |
comp | - | 應用於投影元素的比較 |
proj1 | - | 應用於第一個範圍元素的投影。 |
proj2 | - | 應用於第二個範圍元素的投影。 |
[編輯] 返回值
{last1, last2, result_last},其中 result_last 是構造範圍的末尾。
[編輯] 複雜度
至多 N − 1 次比較和每個投影的應用,其中 N = ranges::distance(first1, last1) + ranges::distance(first2, last12)。
[編輯] 注意
此演算法執行的任務類似於 ranges::set_union。兩者都消耗兩個已排序的輸入範圍,並生成一個包含兩個輸入元素的已排序輸出。這兩個演算法之間的區別在於處理兩個輸入範圍中比較等效的值(參見 LessThanComparable 的注意事項)。如果任何等效值在第一個範圍中出現 n 次,在第二個範圍中出現 m 次,則 ranges::merge 將輸出所有 n + m 次出現,而 ranges::set_union 將只輸出 max(n, m) 次。因此 ranges::merge 精確輸出 N 個值,而 ranges::set_union 可能會產生更少的值。
[編輯] 可能的實現
struct merge_fn { template<std::input_iterator I1, std::sentinel_for<I1> S1, std::input_iterator I2, std::sentinel_for<I2> S2, std::weakly_incrementable O, class Comp = ranges::less, class Proj1 = std::identity, class Proj2 = std::identity> requires std::mergeable<I1, I2, O, Comp, Proj1, Proj2> constexpr ranges::merge_result<I1, I2, O> operator()(I1 first1, S1 last1, I2 first2, S2 last2, O result, Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) const { for (; !(first1 == last1 or first2 == last2); ++result) { if (std::invoke(comp, std::invoke(proj2, *first2), std::invoke(proj1, *first1))) *result = *first2, ++first2; else *result = *first1, ++first1; } auto ret1{ranges::copy(std::move(first1), std::move(last1), std::move(result))}; auto ret2{ranges::copy(std::move(first2), std::move(last2), std::move(ret1.out))}; return {std::move(ret1.in), std::move(ret2.in), std::move(ret2.out)}; } template<ranges::input_range R1, ranges::input_range R2, std::weakly_incrementable O, class Comp = ranges::less, class Proj1 = std::identity, class Proj2 = std::identity> requires std::mergeable<ranges::iterator_t<R1>, ranges::iterator_t<R2>, O, Comp, Proj1, Proj2> constexpr ranges::merge_result<ranges::borrowed_iterator_t<R1>, ranges::borrowed_iterator_t<R2>, O> operator()(R1&& r1, R2&& r2, O result, Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) const { return (*this)(ranges::begin(r1), ranges::end(r1), ranges::begin(r2), ranges::end(r2), std::move(result), std::move(comp), std::move(proj1), std::move(proj2)); } }; inline constexpr merge_fn merge {}; |
[編輯] 示例
#include <algorithm> #include <iostream> #include <iterator> #include <vector> void print(const auto& in1, const auto& in2, auto first, auto last) { std::cout << "{ "; for (const auto& e : in1) std::cout << e << ' '; std::cout << "} +\n{ "; for (const auto& e : in2) std::cout << e << ' '; std::cout << "} =\n{ "; while (!(first == last)) std::cout << *first++ << ' '; std::cout << "}\n\n"; } int main() { std::vector<int> in1, in2, out; in1 = {1, 2, 3, 4, 5}; in2 = {3, 4, 5, 6, 7}; out.resize(in1.size() + in2.size()); const auto ret = std::ranges::merge(in1, in2, out.begin()); print(in1, in2, out.begin(), ret.out); in1 = {1, 2, 3, 4, 5, 5, 5}; in2 = {3, 4, 5, 6, 7}; out.clear(); out.reserve(in1.size() + in2.size()); std::ranges::merge(in1, in2, std::back_inserter(out)); print(in1, in2, out.cbegin(), out.cend()); }
輸出
{ 1 2 3 4 5 } + { 3 4 5 6 7 } = { 1 2 3 3 4 4 5 5 6 7 } { 1 2 3 4 5 5 5 } + { 3 4 5 6 7 } = { 1 2 3 3 4 4 5 5 5 5 6 7 }
[編輯] 另請參閱
(C++20) |
就地歸併兩個有序範圍 (演算法函式物件) |
(C++20) |
檢查一個範圍是否按升序排序 (演算法函式物件) |
(C++20) |
計算兩個集合的並集 (演算法函式物件) |
(C++20) |
將一個範圍按升序排序 (演算法函式物件) |
(C++20) |
對一個範圍的元素進行排序,同時保留相等元素之間的順序 (演算法函式物件) |
歸併兩個已排序的範圍 (函式模板) |