std::set_intersection
| ヘッダー <algorithm> で定義 |
||
template< class InputIt1, class InputIt2, class OutputIt > OutputIt set_intersection( InputIt1 first1, InputIt1 last1, |
(1) | (C++20 以降 constexpr) |
| template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class ForwardIt3 > |
(2) | (C++17以降) |
template< class InputIt1, class InputIt2, class OutputIt, class Compare > |
(3) | (C++20 以降 constexpr) |
| template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, |
(4) | (C++17以降) |
d_firstから始まる、ソート済みの範囲 [first1, last1) とソート済みの範囲 [first2, last2) の両方に含まれる要素からなる、ソート済みの範囲を構築します。
もし [first1, last1) が互いに等価な m 個の要素を含み、[first2, last2) がそれらと等価な n 個の要素を含む場合、[first1, last1) からの最初の std::min(m, n) 個の要素が出力範囲にコピーされ、順序が保持されます。
[first1, last1) または [first2, last2) が operator<(until C++20)std::less{}(since C++20)に関してソートされていない場合、動作は未定義です。[first1, last1) または [first2, last2) が comp に関してソートされていない場合、動作は未定義です。|
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) と重複する場合、動作は未定義です。
目次 |
[edit] パラメータ
| first1, last1 | - | 検査する最初のソート済み要素の範囲を定義するイテレータのペア |
| first2, last2 | - | 検査する2番目のソート済み要素の範囲を定義するイテレータのペア |
| d_first | - | 出力範囲の開始位置 |
| policy | - | 使用する 実行ポリシー |
| comp | - | 比較関数オブジェクト(つまり、Compare の要件を満たすオブジェクト)。最初の引数が2番目の引数より小さい(つまり、前に順序付けられる)場合に true を返します。 比較関数のシグネチャは、以下と同等でなければならない。 bool cmp(const Type1& a, const Type2& b); シグネチャは const& を持つ必要はありませんが、関数は渡されたオブジェクトを変更してはならず、値カテゴリ に関係なく、(おそらく const の) |
| 型要件 | ||
-InputIt1, InputIt2 は LegacyInputIterator の要件を満たす必要がある。 | ||
-OutputIt は LegacyOutputIterator の要件を満たさなければなりません。 | ||
-ForwardIt1, ForwardIt2, ForwardIt3 は LegacyForwardIterator の要件を満たしている必要があります。 | ||
-CompareはCompareの要件を満たす必要がある。 | ||
[edit] 戻り値
構築された範囲の終了位置の次を指すイテレータ。
[edit] 計算量
std::distance(first1, last1) を N1 とし、std::distance(first2, last2) を N2 とする。
[edit] 例外
ExecutionPolicy というテンプレートパラメータを持つオーバーロードは、次のようにエラーを報告します。
- アルゴリズムの一部として呼び出された関数の実行が例外をスローし、
ExecutionPolicyが 標準ポリシー のいずれかである場合、std::terminate が呼び出されます。その他のExecutionPolicyの場合、動作は実装定義です。 - アルゴリズムがメモリの割り当てに失敗した場合、std::bad_alloc がスローされます。
[edit] 可能な実装
| set_intersection (1) |
|---|
template<class InputIt1, class InputIt2, class OutputIt> OutputIt set_intersection(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt d_first) { while (first1 != last1 && first2 != last2) { if (*first1 < *first2) ++first1; else { if (!(*first2 < *first1)) *d_first++ = *first1++; // *first1 and *first2 are equivalent. ++first2; } } return d_first; } |
| set_intersection (3) |
template<class InputIt1, class InputIt2, class OutputIt, class Compare> OutputIt set_intersection(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt d_first, Compare comp) { while (first1 != last1 && first2 != last2) { if (comp(*first1, *first2)) ++first1; else { if (!comp(*first2, *first1)) *d_first++ = *first1++; // *first1 and *first2 are equivalent. ++first2; } } return d_first; } |
[edit] 例
#include <algorithm> #include <iostream> #include <iterator> #include <vector> int main() { std::vector<int> v1{7, 2, 3, 4, 5, 6, 7, 8}; std::vector<int> v2{5, 7, 9, 7}; std::sort(v1.begin(), v1.end()); std::sort(v2.begin(), v2.end()); std::vector<int> v_intersection; std::set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), std::back_inserter(v_intersection)); for (int n : v_intersection) std::cout << n << ' '; std::cout << '\n'; }
出力
5 7 7
[edit] 不具合報告
以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。
| DR | 適用対象 | 公開された動作 | 正しい動作 |
|---|---|---|---|
| LWG 291 | C++98 | 入力範囲における等価な要素の扱いが未指定でした。 | 指定された |
[edit] 関連項目
| 2つの集合の和を計算する (関数テンプレート) | |
| (C++20) |
2つの集合の積を計算する (アルゴリズム関数オブジェクト) |