Namespaces
Variants

std::ranges:: set_difference, std::ranges:: set_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_difference_result < I1, O >
set_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_difference_result < ranges:: borrowed_iterator_t < R1 > , O >
set_difference ( R1 && r1, R2 && r2, O result, Comp comp = { } ,

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

ソート済み入力範囲 [ first1 , last1 ) に含まれ、ソート済み入力範囲 [ first2 , last2 ) には含まれない要素を、 result を先頭とする出力範囲にコピーします。

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

  • 入力範囲がそれぞれ comp および proj1 または proj2 に関してソートされていない場合、または
  • 結果の範囲がいずれかの入力範囲と重複している場合。
1) 要素は指定された二項比較関数 comp を使用して比較されます。
2) (1) と同様だが、 r1 を第一範囲、 r2 を第二範囲として使用する。すなわち 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, result_last } 、ここで result_last は構築された範囲の終端を指します。

計算量

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

実装例

struct set_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_difference_result<I1, 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)))
                ++first2;
            else
            {
                ++first1;
                ++first2;
            }
        }
        return ranges::copy(std::move(first1), std::move(last1), std::move(result));
    }
    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_difference_result<ranges::borrowed_iterator_t<R1>, 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_difference_fn set_difference {};
**注記**: 提供されたC++コードはすべてのHTMLタグ、属性、および` `、`
`タグ内のテキストを保持したまま、C++固有の用語を翻訳せずに正確に保存されています。コード自体は翻訳対象外のため、元のまま維持されています。

#include <algorithm>
#include <cassert>
#include <iostream>
#include <iterator>
#include <string_view>
#include <vector>
auto print = [](const auto& v, std::string_view end = "")
{
    std::cout << "{ ";
    for (auto n{v.size()}; auto i : v)
        std::cout << i << (--n ? ", " : " ");
    std::cout << "} " << end;
};
struct Order // 非常に興味深いデータを持つ構造体
{
    int order_id{};
    friend std::ostream& operator<<(std::ostream& os, const Order& ord)
    {
        return os << '{' << ord.order_id << '}';
    }
};
int main()
{
    const auto v1 = {1, 2, 5, 5, 5, 9};
    const auto v2 = {2, 5, 7};
    std::vector<int> diff{};
    std::ranges::set_difference(v1, v2, std::back_inserter(diff));
    print(v1, "∖ ");
    print(v2, "= ");
    print(diff, "\n\n");
    // 古い状態と新しい状態の間でどの注文が「削除」されたかを知りたい:
    const std::vector<Order> old_orders{{1}, {2}, {5}, {9}};
    const std::vector<Order> new_orders{{2}, {5}, {7}};
    std::vector<Order> cut_orders(old_orders.size() + new_orders.size());
    auto [old_orders_end, cut_orders_last] =
        std::ranges::set_difference(old_orders, new_orders,
                                    cut_orders.begin(), {},
                                    &Order::order_id, &Order::order_id);
    assert(old_orders_end == old_orders.end());
    std::cout << "old orders = ";
    print(old_orders, "\n");
    std::cout << "new orders = ";
    print(new_orders, "\n");
    std::cout << "cut orders = ";
    print(cut_orders, "\n");
    cut_orders.erase(cut_orders_last, end(cut_orders));
    std::cout << "cut orders = ";
    print(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}, {0}, {0}, {0}, {0}, {0} } 
cut orders = { {1}, {9} }

関連項目

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