std::set_union
| ヘッダー <algorithm> で定義 |
||
template< class InputIt1, class InputIt2, class OutputIt > OutputIt set_union( 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) からの m 個の要素すべてが順序を保ったまま出力範囲にコピーされ、その後、[first2, last2) からの std::max(n - m, 0) 個の要素が順序を保ったまま出力範囲にコピーされます。
[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 の要件を満たす必要がある。 | ||
-ForwardIt1, ForwardIt2, ForwardIt3 は LegacyForwardIterator の要件を満たしている必要があります。 | ||
-OutputIt は LegacyOutputIterator の要件を満たさなければなりません。 | ||
-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_union (1) |
|---|
template<class InputIt1, class InputIt2, class OutputIt> OutputIt set_union(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt d_first) { for (; first1 != last1; ++d_first) { if (first2 == last2) return std::copy(first1, last1, d_first); if (*first2 < *first1) *d_first = *first2++; else { *d_first = *first1; if (!(*first1 < *first2)) ++first2; ++first1; } } return std::copy(first2, last2, d_first); } |
| set_union (3) |
template<class InputIt1, class InputIt2, class OutputIt, class Compare> OutputIt set_union(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt d_first, Compare comp) { for (; first1 != last1; ++d_first) { if (first2 == last2) // Finished range 2, include the rest of range 1: return std::copy(first1, last1, d_first); if (comp(*first2, *first1)) *d_first = *first2++; else { *d_first = *first1; if (!comp(*first1, *first2)) // Equivalent => don't need to include *first2. ++first2; ++first1; } } // Finished range 1, include the rest of range 2: return std::copy(first2, last2, d_first); } |
[edit] 注釈
このアルゴリズムは、std::merge と同様のタスクを実行します。どちらも2つのソート済み入力範囲を消費し、両方の入力からの要素を持つソート済みの出力を生成します。これらの2つのアルゴリズムの違いは、両方の入力範囲からの等価な値の扱いにあります(LessThanComparable の注釈を参照)。もし、最初の範囲に n 回、2番目の範囲に m 回、等価な値が現れた場合、std::merge は n + m 回の出現すべてを出力しますが、std::set_union は std::max(n, m) 回のみを出力します。したがって、std::merge は正確に std::distance(first1, last1) + std::distance(first2, last2) 個の値を生成し、std::set_union はそれより少ない値を生成する可能性があります。
[edit] 例
#include <algorithm> #include <iostream> #include <iterator> #include <vector> void println(const std::vector<int>& v) { for (int i : v) std::cout << i << ' '; std::cout << '\n'; } int main() { std::vector<int> v1, v2, dest; v1 = {1, 2, 3, 4, 5}; v2 = {3, 4, 5, 6, 7}; std::set_union(v1.cbegin(), v1.cend(), v2.cbegin(), v2.cend(), std::back_inserter(dest)); println(dest); dest.clear(); v1 = {1, 2, 3, 4, 5, 5, 5}; v2 = {3, 4, 5, 6, 7}; std::set_union(v1.cbegin(), v1.cend(), v2.cbegin(), v2.cend(), std::back_inserter(dest)); println(dest); }
出力
1 2 3 4 5 6 7 1 2 3 4 5 5 5 6 7
[edit] 不具合報告
以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。
| DR | 適用対象 | 公開された動作 | 正しい動作 |
|---|---|---|---|
| LWG 291 | C++98 | 入力範囲における等価な要素の扱いが未指定でした。 | 指定された |
[edit] 関連項目
| あるシーケンスが別のシーケンスの部分シーケンスである場合に true を返す (関数テンプレート) | |
| 2つのソート済み範囲をマージする (関数テンプレート) | |
| 2つの集合の差を計算する (関数テンプレート) | |
| 2つの集合の積を計算する (関数テンプレート) | |
| 2つの集合の対称差を計算する (関数テンプレート) | |
| (C++20) |
2つの集合の和を計算する (アルゴリズム関数オブジェクト) |