名前空間
変種
操作

std::set_difference

From cppreference.com
< cpp‎ | algorithm
 
 
アルゴリズムライブラリ
制約付きアルゴリズムとRangeアルゴリズム (C++20)
制約付きアルゴリズム、例: ranges::copy, ranges::sort, ...
実行ポリシー (C++17)
シーケンスを変更しない操作
一括操作
(C++17)
検索操作
(C++11)                (C++11)(C++11)

シーケンスを変更する操作
コピー操作
(C++11)
(C++11)
スワップ操作
変換操作
生成操作
削除操作
順序変更操作
(C++17まで)(C++11)
(C++20)(C++20)
サンプリング操作
(C++17)

ソートおよび関連操作
パーティション操作
ソート操作
二分探索操作
(パーティション化された範囲)
集合操作 (ソート済み範囲)
set_difference

マージ操作 (ソート済み範囲)
ヒープ操作
最小/最大操作
(C++11)
(C++17)
辞書順比較操作
順列操作
Cライブラリ
数値演算
未初期化メモリに対する操作
 
ヘッダー <algorithm> で定義
template< class InputIt1, class InputIt2, class OutputIt >

OutputIt set_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_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_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_difference( ExecutionPolicy&& policy,
                           ForwardIt1 first1, ForwardIt1 last1,
                           ForwardIt2 first2, ForwardIt2 last2,

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

ソート済みの範囲 [first1last1) から、ソート済みの範囲 [first2last2) には存在しない要素を、 d_first で始まる範囲にコピーします。出力範囲もソートされます。

もし [first1last1) が互いに等価な m 個の要素を含み、 [first2last2) がそれらに等価な n 個の要素を含む場合、 std::max(m - n, 0) 個の要素が [first1last1) から出力範囲にコピーされ、順序が保持されます。

1) もし [first1last1) または [first2last2)operator<(until C++20)std::less{}(since C++20) に対して ソートされていない場合、動作は未定義です。
3) もし [first1last1) または [first2last2)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以降)

出力範囲が [first1last1) または [first2last2) と重複する場合、動作は未定義です。

目次

[edit] パラメータ

first1, last1 - 調べる最初の入力ソート済み 範囲を定義するイテレータのペア
first2, last2 - 検索対象となる2番目の入力ソート済み 範囲を定義するイテレータのペア
d_first - 出力範囲の開始位置
policy - 使用する 実行ポリシー
comp - 比較関数オブジェクト(つまり、Compare の要件を満たすオブジェクト)。最初の引数が2番目の引数より小さい(つまり、前に順序付けられる)場合に true を返します。

比較関数のシグネチャは、以下と同等でなければならない。

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

シグネチャは const& を持つ必要はありませんが、関数は渡されたオブジェクトを変更してはならず、値カテゴリ に関係なく、(おそらく const の) Type1Type2 のすべての値を受け入れられる必要があります (したがって、Type1& は許可されません。また、Type1 のムーブがコピーと同等でない限り、Type1 も許可されません(C++11以降))。
Type1 および Type2 は、 InputIt1 および InputIt2 のオブジェクトを逆参照し、その後両方の Type1 および Type2 に暗黙的に変換できる必要があります。

型要件
-
InputIt1, InputIt2LegacyInputIterator の要件を満たす必要がある。
-
OutputItLegacyOutputIterator の要件を満たさなければなりません。
-
ForwardIt1, ForwardIt2, ForwardIt3LegacyForwardIterator の要件を満たしている必要があります。
-
CompareCompareの要件を満たす必要がある。

[edit] 戻り値

構築された範囲の終了位置の次を指すイテレータ。

[edit] 計算量

std::distance(first1, last1)N1 とし、std::distance(first2, last2)N2 とする。

1,2) 2⋅(N1+N2)-1 回以下の比較 (operator<(until C++20)std::less{}(since C++20)).
3,4) 比較関数 comp の適用は 2⋅(N1+N2)-1 回以下。

[edit] 例外

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

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

[edit] 可能な実装

set_difference (1)
template<class InputIt1, class InputIt2, class OutputIt>
OutputIt set_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))
                ++first1;
            ++first2;
        }
    }
    return d_first;
}
set_difference (3)
template<class InputIt1, class InputIt2, class OutputIt, class Compare>
OutputIt set_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))
                ++first1;
            ++first2;
        }
    }
    return d_first;
}

[edit]

#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
 
template<typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v)
{
    os << '{';
    for (auto n{v.size()}; const auto& e : v)
        os << e << (--n ? ", " : "");
    return os << '}';
}
 
struct Order // a struct with very interesting data
{
    int order_id{};
 
    friend std::ostream& operator<<(std::ostream& os, const Order& ord)
    {
        return os << ord.order_id;
    }
};
 
int main()
{
    const std::vector<int> v1{1, 2, 5, 5, 5, 9};
    const std::vector<int> v2{2, 5, 7};
    std::vector<int> diff;
 
    std::set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(),
                        std::inserter(diff, diff.begin()));
 
    std::cout << v1 << " ∖ " << v2 << " == " << diff << "\n\n";
 
    // we want to know which orders "cut" between old and new states:
    std::vector<Order> old_orders{{1}, {2}, {5}, {9}};
    std::vector<Order> new_orders{{2}, {5}, {7}};
    std::vector<Order> cut_orders;
 
    std::set_difference(old_orders.begin(), old_orders.end(),
                        new_orders.begin(), new_orders.end(),
                        std::back_inserter(cut_orders),
                        [](auto& a, auto& b) { return a.order_id < b.order_id; });
 
    std::cout << "old orders: " << old_orders << '\n'
              << "new orders: " << new_orders << '\n'
              << "cut orders: " << cut_orders << '\n';
}

出力

{1, 2, 5, 5, 5, 9} ∖ {2, 5, 7} == {1, 5, 5, 9}
 
old orders: {1, 2, 5, 9}
new orders: {2, 5, 7}
cut orders: {1, 9}

[edit] 不具合報告

以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。

DR 適用対象 公開された動作 正しい動作
LWG 291 C++98 入力範囲における等価な要素の扱いが未指定でした。 指定された

[edit] 関連項目

あるシーケンスが別のシーケンスの部分シーケンスである場合に true を返す
(関数テンプレート) [編集]
2つの集合の対称差を計算する
(関数テンプレート) [編集]
2つの集合の差を計算する
(アルゴリズム関数オブジェクト)[編集]
English 日本語 中文(简体) 中文(繁體)