Namespaces
Variants

std::ranges:: set_symmetric_difference, std::ranges:: set_symmetric_difference_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_symmetric_difference_result < I1, I2, O >
set_symmetric_difference ( 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_symmetric_difference_result < ranges:: borrowed_iterator_t < R1 > ,
ranges:: borrowed_iterator_t < R2 > , O >
set_symmetric_difference ( R1 && r1, R2 && r2, O result, Comp comp = { } ,

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

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

ある要素が m [ first1 , last1 ) に現れ、 n [ first2 , last2 ) に現れる場合、その要素は result に正確に │m - n│ 回コピーされます。 m > n の場合、それらの要素の最後の m - n 個が [ first1 , last1 ) からコピーされ、そうでない場合は最後の n - m 個の要素が [ first2 , last2 ) からコピーされます。結果の範囲は入力範囲のいずれとも重なってはなりません。

動作は未定義である、もし

  • 入力範囲がそれぞれ 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 ) です。

実装例

struct set_symmetric_difference_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_symmetric_difference_result<I1, I2, O>
        operator()(I1 first1, S1 last1, I2 first2, S2 last2, O result, Comp comp = {},
                   Proj1 proj1 = {}, Proj2 proj2 = {}) const
    {
        while (!(first1 == last1 or first2 == last2))
        {
            if (std::invoke(comp, std::invoke(proj1, *first1), std::invoke(proj2, *first2)))
            {
                *result = *first1;
                ++first1;
                ++result;
            }
            else if (std::invoke(comp, std::invoke(proj2, *first2),
                                       std::invoke(proj1, *first1)))
            {
                *result = *first2;
                ++first2;
                ++result;
            }
            else
            {
                ++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_symmetric_difference_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_symmetric_difference_fn set_symmetric_difference {};

#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
void visualize_this(const auto& v, int min = 1, int max = 9)
{
    for (auto i {min}; i <= max; ++i)
    {
        std::ranges::binary_search(v, i) ? std::cout << i : std::cout << '.';
        std::cout << ' ';
    }
    std::cout << '\n';
}
int main()
{
    const auto in1 = {1, 3, 4,    6, 7, 9};
    const auto in2 = {1,    4, 5, 6,    9};
    std::vector<int> out {};
    std::ranges::set_symmetric_difference(in1, in2, std::back_inserter(out));
    visualize_this(in1);
    visualize_this(in2);
    visualize_this(out);
}

出力:

1 . 3 4 . 6 7 . 9
1 . . 4 5 6 . . 9
. . 3 . 5 . 7 . .

関連項目

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