Namespaces
Variants

std::ranges:: set_union, std::ranges:: set_union_result

From cppreference.net
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy , ranges::sort , ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17) (C++11)
(C++20) (C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
Constrained algorithms
All names in this menu belong to namespace std::ranges
Non-modifying sequence operations
Modifying sequence operations
Partitioning operations
Sorting operations
Binary search operations (on sorted ranges)
Set operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Permutation operations
Fold operations
Operations on uninitialized storage
Return types
(注:このHTML要素には翻訳対象のテキストコンテンツが含まれていないため、元の構造を保持したまま出力します)
定義済みヘッダー <algorithm>
呼び出しシグネチャ
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 set_union_result < I1, I2, O >
set_union ( I1 first1, S1 last1, I2 first2, S2 last2,
O result, Comp comp = { } ,

Proj1 proj1 = { } , Proj2 proj2 = { } ) ;
(1) (C++20以降)
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 set_union_result < ranges:: borrowed_iterator_t < R1 > ,
ranges:: borrowed_iterator_t < R2 > , O >
set_union ( R1 && r1, R2 && r2, O result, Comp comp = { } ,

Proj1 proj1 = { } , Proj2 proj2 = { } ) ;
(2) (C++20以降)
ヘルパー型
template < class I1, class I2, class O >
using set_union_result = ranges:: in_in_out_result < I1, I2, O > ;
(3) (C++20以降)

ソート済みの和集合を result から開始して構築します。これは、一方または両方のソート済み入力範囲 [ first1 , last1 ) および [ first2 , last2 ) に存在する要素の集合から成ります。

ある要素が m [ first1 , last1 ) に現れ、 n [ first2 , last2 ) に現れる場合、 m 個の要素すべてが順序を保って [ first1 , last1 ) から result にコピーされ、その後、正確に max ( n - m, 0 ) 個の要素が順序を保って [ first2 , last2 ) から result にコピーされます。

以下の場合、動作は未定義です。

  • 入力範囲がそれぞれ comp および proj1 または proj2 に関してソートされていない場合、または
  • 結果の範囲が入力範囲のいずれかと重複している場合。
1) 要素は指定された二項比較関数 comp を使用して比較されます。
2) (1) と同様だが、 r1 を最初の範囲として、 r2 を2番目の範囲として使用する。具体的には ranges:: begin ( r1 ) first1 として、 ranges:: end ( r1 ) last1 として、 ranges:: begin ( r2 ) first2 として、 ranges:: end ( r2 ) last2 として使用する場合と同等である。

このページで説明されている関数ライクなエンティティは、 アルゴリズム関数オブジェクト (非公式には niebloids として知られる)です。すなわち:

目次

パラメータ

first1, last1 - 要素の最初の入力ソート済み 範囲 を定義するイテレータ-番兵ペア
first2, last2 - 要素の2番目の入力ソート済み 範囲 を定義するイテレータ-番兵ペア
r1 - 最初の入力ソート済み範囲
r2 - 2番目の入力ソート済み範囲
result - 出力範囲の先頭
comp - 投影された要素に適用する比較
proj1 - 最初の範囲の要素に適用する投影
proj2 - 2番目の範囲の要素に適用する投影

戻り値

{ last1, last2, result_last } 、ここで result_last は構築された範囲の終端を指します。

計算量

最大で 2·(N 1 +N 2 )-1 回の比較と各射影の適用が行われます。ここで N 1 N 2 はそれぞれ ranges:: distance ( first1, last1 ) ranges:: distance ( first2, last2 ) です。

注記

このアルゴリズムは ranges::merge と同様の処理を実行します。両アルゴリズムとも2つのソート済み入力範囲を消費し、両入力からの要素を含むソート済み出力を生成します。これらのアルゴリズムの違いは、等価と比較される両入力範囲の値の扱いにあります( LessThanComparable の注記を参照)。等価な値が最初の範囲に n 回、2番目の範囲に m 回現れた場合、 ranges::merge は全ての n + m 個の出現を出力しますが、 ranges::set_union std:: max ( n, m ) 個のみを出力します。したがって ranges::merge は正確に (N 1 +N 2 ) 個の値を出力しますが、 ranges::set_union はより少ない出力を生成する可能性があります。

実装例

struct set_union_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::set_union_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(proj1, *first1), 
                                  std::invoke(proj2, *first2)))
            {
                *result = *first1;
                ++first1;
            }
            else if (std::invoke(comp, std::invoke(proj2, *first2),
                                       std::invoke(proj1, *first1)))
            {
                *result = *first2;
                ++first2;
            }
            else
            {
                *result = *first1;
                ++first1;
                ++first2;
            }
        }
        auto res1 = ranges::copy(std::move(first1), std::move(last1), std::move(result));
        auto res2 = ranges::copy(std::move(first2), std::move(last2), std::move(res1.out));
        return {std::move(res1.in), std::move(res2.in), std::move(res2.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::set_union_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 set_union_fn set_union {};

#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 << "} ∪ { ";
    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::set_union(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::set_union(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 4 5 6 7 }
{ 1 2 3 4 5 5 5 } ∪ { 3 4 5 6 7 } =
{ 1 2 3 4 5 5 5 6 7 }

関連項目

2つの集合の差を計算する
(アルゴリズム関数オブジェクト)
2つの集合の積を計算する
(アルゴリズム関数オブジェクト)
2つの集合の対称差を計算する
(アルゴリズム関数オブジェクト)
2つのソート済み範囲をマージする
(アルゴリズム関数オブジェクト)
あるシーケンスが別のシーケンスの部分シーケンスである場合に true を返す
(アルゴリズム関数オブジェクト)
2つの集合の和を計算する
(関数テンプレート)