Namespaces
Variants

std:: set_symmetric_difference

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)
set_symmetric_difference

Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
ヘッダーで定義 <algorithm>
template < class InputIt1, class InputIt2, class OutputIt >

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

OutputIt d_first ) ;
(1) (C++20以降constexpr)
template < class ExecutionPolicy,

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

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

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

OutputIt d_first, Compare comp ) ;
(3) (C++20以降constexpr)
template < class ExecutionPolicy,

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

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

二つのソート済み範囲の対称差を計算します: どちらか一方の範囲に存在するが両方には存在しない要素が、 d_first から始まる範囲にコピーされます。出力範囲もソートされています。

[ first1 , last1 ) が互いに等価な m 個の要素を含み、 [ first2 , last2 ) がそれらと等価な n 個の要素を含む場合、 std :: abs ( m - n ) 個の要素が順序を保持して出力範囲にコピーされます:

  • if m > n の場合、 m - n 個の末尾要素が [ first1 , last1 ) から取得されます。
  • if m < n の場合、 n - m 個の末尾要素が [ first2 , last2 ) から取得されます。
1) [ first1 , last1 ) または [ first2 , last2 ) のいずれかが ソート済み でない場合 operator < (C++20以前) std:: less { } (C++20以降) 、動作は未定義です。
3) first1 last1 または first2 last2 の範囲が comp に関してソートされていない場合、動作は未定義です。
2,4) (1,3) と同様ですが、 policy に従って実行されます。
これらのオーバーロードは、以下の全ての条件が満たされる場合にのみオーバーロード解決に参加します:

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以降)

出力範囲が [ first1 , last1 ) または [ first2 , last2 ) と重なる場合、動作は未定義です。

目次

パラメータ

first1, last1 - 要素の最初のソート済み 範囲 を定義するイテレータのペア
first2, last2 - 要素の2番目のソート済み 範囲 を定義するイテレータのペア
d_first - 出力範囲の先頭
policy - 使用する 実行ポリシー
comp - 比較関数オブジェクト(つまり、 Compare の要件を満たすオブジェクト)。最初の引数が2番目の引数より 小さい (つまり、順序付けにおいて 前に来る )場合に​ true を返す。

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

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

シグネチャが const & を持つ必要はないが、関数は渡されたオブジェクトを変更してはならず、 値カテゴリ に関係なく(したがって、 Type1& は許可されない Type1 も同様、ただし Type1 に対してムーブがコピーと等価である場合を除く (C++11以降) )、すべての Type1 Type2 型の値を受け入れられなければならない。
Type1 Type2 は、 InputIt1 InputIt2 型のオブジェクトが間接参照可能であり、その後 Type1 Type2 の両方に暗黙的に変換可能でなければならない。 ​

型要件
-
InputIt1, InputIt2 LegacyInputIterator の要件を満たさなければならない。
-
OutputIt LegacyOutputIterator の要件を満たさなければならない。
-
ForwardIt1, ForwardIt2, ForwardIt3 LegacyForwardIterator の要件を満たさなければならない。
-
Compare Compare の要件を満たさなければならない。

戻り値

構築された範囲の終端を超えるイテレータ。

計算量

N 1 std:: distance ( first1, last1 ) として、 N 2 std:: distance ( first2, last2 ) として定義する:

1,2) 最大 2⋅(N 1 +N 2 )-1 回の比較を operator < (C++20以前) std:: less { } (C++20以降) を使用して行う。
3,4) 最大 2⋅(N 1 +N 2 )-1 回の比較関数 comp の適用。

例外

ExecutionPolicy という名前のテンプレートパラメータを持つオーバーロードは、 以下のようにエラーを報告します:

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

実装例

set_symmetric_difference (1)
template<class InputIt1, class InputIt2, class OutputIt>
OutputIt set_symmetric_difference(InputIt1 first1, InputIt1 last1,
                                  InputIt2 first2, InputIt2 last2, OutputIt d_first)
{
    while (first1 != last1)
    {
        if (first2 == last2)
            return std::copy(first1, last1, d_first);
        if (*first1 < *first2)
            *d_first++ = *first1++;
        else
        {
            if (*first2 < *first1)
                *d_first++ = *first2;
            else
                ++first1;
            ++first2;
        }
    }
    return std::copy(first2, last2, d_first);
}
set_symmetric_difference (3)
template<class InputIt1, class InputIt2, class OutputIt, class Compare>
OutputIt set_symmetric_difference(InputIt1 first1, InputIt1 last1,
                                  InputIt2 first2, InputIt2 last2,
                                  OutputIt d_first, Compare comp)
{
    while (first1 != last1)
    {
        if (first2 == last2)
            return std::copy(first1, last1, d_first);
        if (comp(*first1, *first2))
            *d_first++ = *first1++;
        else
        {
            if (comp(*first2, *first1))
                *d_first++ = *first2;
            else
                ++first1;
            ++first2;
        }
    }
    return std::copy(first2, last2, d_first);
}

#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
int main()
{
    std::vector<int> v1{1, 2, 3, 4, 5, 6, 7, 8};
    std::vector<int> v2{5, 7, 9, 10};
    std::sort(v1.begin(), v1.end());
    std::sort(v2.begin(), v2.end());
    std::vector<int> v_symDifference;
    std::set_symmetric_difference(v1.begin(), v1.end(), v2.begin(), v2.end(),
                                  std::back_inserter(v_symDifference));
    for (int n : v_symDifference)
        std::cout << n << ' ';
    std::cout << '\n';
}

出力:

1 2 3 4 6 8 9 10

不具合報告

以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。

DR 適用対象 公開時の動作 正しい動作
LWG 291 C++98 入力範囲における等価な要素の扱い方が規定されていなかった 規定された

関連項目

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