Namespaces
Variants

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

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

ソート済みの2つの入力範囲 [ first1 , last1 ) [ first2 , last2 ) の両方に存在する要素から構成されるソート済み範囲を result から始まる位置に構築します。ある要素が [ first1 , last1 ) m 回、 [ first2 , last2 ) n 回存在する場合、最初の min ( m, n ) 個の要素が最初の範囲から 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 として知られる)です。すなわち:

目次

翻訳の説明: - 「Contents」を「目次」に翻訳しました - C++関連の専門用語(Parameters, Return value, Complexity, Possible implementation, Example, See also)は原文のまま保持しました - HTMLタグ、属性、クラス名、IDなどは一切変更していません - 数値や構造は完全に保持されています

パラメータ

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_intersection_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_intersection_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)))
                ++first1;
            else if (std::invoke(comp, std::invoke(proj2, *first2),
                                       std::invoke(proj1, *first1)))
                ++first2;
            else
                *result = *first1, ++first1, ++first2, ++result;
        }
        return {ranges::next(std::move(first1), std::move(last1)),
                ranges::next(std::move(first2), std::move(last2)),
                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_intersection_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_intersection_fn set_intersection {};

#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
void print(const auto& v, const auto& rem)
{
    std::cout << "{ ";
    for (const auto& e : v)
        std::cout << e << ' ';
    std::cout << '}' << rem;
}
int main()
{
    const auto in1 = {1, 2, 2, 3, 4, 5, 6};
    const auto in2 = {2, 2, 3, 3, 5, 7};
    std::vector<int> out {};
    std::ranges::set_intersection(in1, in2, std::back_inserter(out));
    print(in1, " ∩ "), print(in2, " = "), print(out, "\n");
}

出力:

{ 1 2 2 3 4 5 6 } ∩ { 2 2 3 3 5 7 } = { 2 2 3 5 }

関連項目

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