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
個の要素が含まれる場合、
m
個の要素すべてが
[
first1
,
last1
)
から出力範囲に順序を保持してコピーされ、その後、最終的な
std::
max
(
n
-
m,
0
)
個の要素が
[
first2
,
last2
)
から出力範囲に順序を保持してコピーされます。
[
first1
,
last1
)
または
[
first2
,
last2
)
が
ソート済み
でない場合
operator
<
(C++20以前)
std::
less
{
}
(C++20以降)
に関して、動作は未定義です。
|
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
)
と重なる場合、動作は未定義です。
目次 |
パラメータ
| first1, last1 | - | 要素の最初の入力ソート済み 範囲 を定義するイテレータのペア |
| first2, last2 | - | 要素の2番目の入力ソート済み 範囲 を定義するイテレータのペア |
| d_first | - | 出力範囲の先頭 |
| policy | - | 使用する 実行ポリシー |
| comp | - |
比較関数オブジェクト(つまり、
Compare
の要件を満たすオブジェクト)。最初の引数が2番目の引数より
小さい
(つまり、順序が
前
にある)場合に
true
を返す。
比較関数のシグネチャは以下と同等であるべき: bool cmp ( const Type1 & a, const Type2 & b ) ;
シグネチャが
const
&
を持つ必要はないが、関数は渡されたオブジェクトを変更してはならず、
値カテゴリ
に関係なく(したがって、
|
| 型要件 | ||
-
InputIt1, InputIt2
は
LegacyInputIterator
の要件を満たさなければならない。
|
||
-
ForwardIt1, ForwardIt2, ForwardIt3
は
LegacyForwardIterator
の要件を満たさなければならない。
|
||
-
OutputIt
は
LegacyOutputIterator
の要件を満たさなければならない。
|
||
-
Compare
は
Compare
の要件を満たさなければならない。
|
||
戻り値
構築された範囲の終端を超えるイテレータ。
計算量
N 1 を std:: distance ( first1, last1 ) として、 N 2 を std:: distance ( first2, last2 ) として定義する:
例外
ExecutionPolicy
という名前のテンプレートパラメータを持つオーバーロードは、
以下のようにエラーを報告します:
-
アルゴリズムの一部として呼び出された関数の実行が例外をスローした場合、
ExecutionPolicyが 標準ポリシー のいずれかであるとき、 std::terminate が呼び出されます。それ以外のExecutionPolicyについては、動作は実装定義です。 - アルゴリズムがメモリの確保に失敗した場合、 std::bad_alloc がスローされます。
実装例
| 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) // 範囲2が終了、範囲1の残りを出力: return std::copy(first1, last1, d_first); if (comp(*first2, *first1)) *d_first = *first2++; else { *d_first = *first1; if (!comp(*first1, *first2)) // 等価 => *first2を含める必要なし ++first2; ++first1; } } // 範囲1が終了、範囲2の残りを出力: return std::copy(first2, last2, d_first); } |
注記
このアルゴリズムは
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
はより少ない数を出力する可能性があります。
例
#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
不具合報告
以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。
| DR | 適用対象 | 公開時の動作 | 正しい動作 |
|---|---|---|---|
| LWG 291 | C++98 | 入力範囲内の等価な要素の扱い方が規定されていなかった | 規定された |
関連項目
|
あるシーケンスが別のシーケンスの部分シーケンスである場合に
true
を返す
(関数テンプレート) |
|
|
2つのソート済み範囲をマージする
(関数テンプレート) |
|
|
2つの集合の差を計算する
(関数テンプレート) |
|
|
2つの集合の積集合を計算する
(関数テンプレート) |
|
|
2つの集合の対称差を計算する
(関数テンプレート) |
|
|
(C++20)
|
2つの集合の和集合を計算する
(アルゴリズム関数オブジェクト) |