名前空間
変種
操作

std::set_union

提供: cppreference.com
< cpp‎ | algorithm
 
 
アルゴリズムライブラリ
制約付きアルゴリズムと範囲に対するアルゴリズム (C++20)
コンセプトとユーティリティ: std::sortable, std::projected, ...
制約付きアルゴリズム: std::ranges::copy, std::ranges::sort, ...
実行ポリシー (C++17)
非変更シーケンス操作
(C++11)(C++11)(C++11)
(C++17)
変更シーケンス操作
未初期化記憶域の操作
分割操作
ソート操作
(C++11)
二分探索操作
集合操作 (ソート済み範囲用)
ヒープ操作
(C++11)
最小/最大演算
(C++11)
(C++17)

順列
数値演算
C のライブラリ
 
ヘッダ <algorithm> で定義
(1)
template< class InputIt1, class InputIt2, class OutputIt >

OutputIt set_union( InputIt1 first1, InputIt1 last1,
                    InputIt2 first2, InputIt2 last2,

                    OutputIt d_first );
(C++20未満)
template< class InputIt1, class InputIt2, class OutputIt >

constexpr OutputIt set_union( InputIt1 first1, InputIt1 last1,
                              InputIt2 first2, InputIt2 last2,

                              OutputIt d_first );
(C++20以上)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class ForwardIt3 >

ForwardIt3 set_union( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1,
                    ForwardIt2 first2, ForwardIt2 last2,

                    ForwardIt3 d_first );
(2) (C++17以上)
(3)
template< class InputIt1, class InputIt2,

          class OutputIt, class Compare >
OutputIt set_union( InputIt1 first1, InputIt1 last1,
                    InputIt2 first2, InputIt2 last2,

                    OutputIt d_first, Compare comp );
(C++20未満)
template< class InputIt1, class InputIt2,

          class OutputIt, class Compare >
constexpr OutputIt set_union( InputIt1 first1, InputIt1 last1,
                              InputIt2 first2, InputIt2 last2,

                              OutputIt d_first, Compare comp );
(C++20以上)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2,

          class ForwardIt3, class Compare >
ForwardIt3 set_union( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1,
                    ForwardIt2 first2, ForwardIt2 last2,

                    ForwardIt3 d_first, Compare comp );
(4) (C++17以上)

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

ある要素が [first1, last1) 内に m 個存在し、 [first2, last2) 内に n 個存在する場合、 [first1, last1) の要素が m 個すべて、順序を保ってコピーされ、その後 [first2, last2) の要素がちょうど std::max(n-m, 0) 個、順序を保ってコピーされます。 要素の順序は維持されます。

結果の範囲はいずれかの入力範囲とオーバーラップしていてはなりません。

1) 要素は operator< を用いて比較されます。 範囲は operator< についてソートされていなければなりません。
3) 要素は二項比較関数 comp を用いて比較されます。 範囲は comp についてソートされていなければなりません。
2,4) (1,3) と同じですが、 policy に従って実行されます。 このオーバーロードは、std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> が true である場合にのみ、オーバーロード解決に参加します。

目次

[編集] 引数

first1, last1 - 1つめのソート済み範囲
first2, last2 - 2つめのソート済み範囲
policy - 使用する実行ポリシー。 詳細は実行ポリシーを参照してください
comp - 最初の要素が2番目の要素より小さい (に順序づけられる) 場合に ​true を返す、比較関数オブジェクト (Compare の要件を満たすオブジェクト)。

比較関数のシグネチャは以下と同等なものであるべきです。

 bool cmp(const Type1 &a, const Type2 &b);

シグネチャが const & を持つ必要はありませんが、関数は渡されたオブジェクトを変更してはならず、 Type1 型および Type2 型 (およびそれらの const 修飾された型) のすべての値を値カテゴリに関わらず受理できなければなりません (そのため Type1 & は許されません。 また Type1 に対してムーブがコピーと同等でなければ Type1 も許されません (C++11以上))。
Type1 および Type2 は、 InputIt1 および InputIt2 型のオブジェクトの逆参照からType1Type2 の両方に暗黙に変換可能なものでなければなりません。 ​

型の要件
-
InputIt1, InputIt2LegacyInputIterator の要件を満たさなければなりません。
-
ForwardIt1, ForwardIt2, ForwardIt3LegacyForwardIterator の要件を満たさなければなりません。
-
OutputItLegacyOutputIterator の要件を満たさなければなりません。

[編集] 戻り値

構築された範囲の終端を指すイテレータ。

[編集] 計算量

多くとも 2·(N1+N2-1) 回の比較、ただし N1 = std::distance(first1, last1)N2 = std::distance(first2, last2) です。

[編集] 例外

テンプレート引数 ExecutionPolicy を持つオーバーロードは以下のようにエラーを報告します。

  • アルゴリズムの一部として呼び出された関数の実行が例外を投げ、 ExecutionPolicy標準のポリシーのいずれかの場合は、 std::terminate が呼ばれます。 それ以外のあらゆる ExecutionPolicy については、動作は処理系定義です。
  • アルゴリズムがメモリの確保に失敗した場合は、 std::bad_alloc が投げられます。

[編集] ノート

このアルゴリズムは std::merge と似た処理を行います。 どちらも2つのソート済み範囲を消費し、両方の入力の要素を持つソート済みの出力を生成します。 これらの2つのアルゴリズムの違いは、両方の入力範囲にある同等 (LessThanComparable のノートを参照) な値の処理方法です。 同等な値が1つめの範囲に n 個現れ、2つめの範囲に m 個現れた場合、 std::mergen+m 個をすべて出力しますが、 std::set_unionstd::max(n, m) 個しか出力しません。 そのため、 std::merge はちょうど std::distance(first1, last1) + std::distance(first2, last2) 個の値を出力しますが、 std::set_union の出力はそれより少なくなる場合があります。

[編集] 実装例

1つめのバージョン
template<class InputIt1, class InputIt2, class OutputIt>
OutputIt set_union(InputIt1 first1, InputIt1 last1,
                   InputIt2 first2, InputIt2 last2,
                   OutputIt d_first)
{
    for (; first1 != last1; ++d_first) {
        if (first2 == last2)
            return std::copy(first1, last1, d_first);
        if (*first2 < *first1) {
            *d_first = *first2++;
        } else {
            *d_first = *first1;
            if (!(*first1 < *first2))
                ++first2;
            ++first1;
        }
    }
    return std::copy(first2, last2, d_first);
}
2つめのバージョン
template<class InputIt1, class InputIt2,
         class OutputIt, class Compare>
OutputIt set_union(InputIt1 first1, InputIt1 last1,
                   InputIt2 first2, InputIt2 last2,
                   OutputIt d_first, Compare comp)
{
    for (; first1 != last1; ++d_first) {
        if (first2 == last2)
            return std::copy(first1, last1, d_first);
        if (comp(*first2, *first1)) {
            *d_first = *first2++;
        } else {
            *d_first = *first1;
            if (!comp(*first1, *first2))
                ++first2;
            ++first1;
        }
    }
    return std::copy(first2, last2, d_first);
}

[編集]

ベクタを使った例。

#include <vector>
#include <iostream>
#include <algorithm>
#include <iterator>
 
int main()
{
    {
        std::vector<int> v1 = {1, 2, 3, 4, 5}; 
        std::vector<int> v2 = {      3, 4, 5, 6, 7}; 
        std::vector<int> dest1;
 
        std::set_union(v1.begin(), v1.end(),
                       v2.begin(), v2.end(),                  
                       std::back_inserter(dest1));
 
        for (const auto &i : dest1) {
            std::cout << i << ' ';
        }   
        std::cout << '\n';
    }
    {
        std::vector<int> v1 = {1, 2, 3, 4, 5, 5, 5}; 
        std::vector<int> v2 = {      3, 4, 5, 6, 7}; 
        std::vector<int> dest1;
 
        std::set_union(v1.begin(), v1.end(),
                       v2.begin(), v2.end(),                  
                       std::back_inserter(dest1));
 
        for (const auto &i : dest1) {
            std::cout << i << ' ';
        }   
        std::cout << '\n';
    }
}

出力:

1 2 3 4 5 6 7 
1 2 3 4 5 5 5 6 7

[編集] 関連項目

ある集合が別の集合の部分集合であるかどうか調べます
(関数テンプレート) [edit]
2つのソート済み範囲をマージします
(関数テンプレート) [edit]
2つの集合の差を計算します
(関数テンプレート) [edit]
2つの集合の交叉を計算します
(関数テンプレート) [edit]
2つの集合の対称差を計算します
(関数テンプレート) [edit]