std::set_difference
| ヘッダー <algorithm> で定義 |
||
template< class InputIt1, class InputIt2, class OutputIt > OutputIt set_difference( 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以降) |
ソート済みの範囲 [first1, last1) から、ソート済みの範囲 [first2, last2) には存在しない要素を、 d_first で始まる範囲にコピーします。出力範囲もソートされます。
もし [first1, last1) が互いに等価な m 個の要素を含み、 [first2, last2) がそれらに等価な n 個の要素を含む場合、 std::max(m - n, 0) 個の要素が [first1, last1) から出力範囲にコピーされ、順序が保持されます。
[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_difference (1) |
|---|
template<class InputIt1, class InputIt2, class OutputIt> OutputIt set_difference(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt d_first) { while (first1 != last1) { if (first2 == last2) return std::copy(first1, last1, d_first); if (*first1 < *first2) *d_first++ = *first1++; else { if (! (*first2 < *first1)) ++first1; ++first2; } } return d_first; } |
| set_difference (3) |
template<class InputIt1, class InputIt2, class OutputIt, class Compare> OutputIt set_difference(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt d_first, Compare comp) { while (first1 != last1) { if (first2 == last2) return std::copy(first1, last1, d_first); if (comp(*first1, *first2)) *d_first++ = *first1++; else { if (!comp(*first2, *first1)) ++first1; ++first2; } } return d_first; } |
[edit] 例
#include <algorithm> #include <iostream> #include <iterator> #include <vector> template<typename T> std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) { os << '{'; for (auto n{v.size()}; const auto& e : v) os << e << (--n ? ", " : ""); return os << '}'; } struct Order // a struct with very interesting data { int order_id{}; friend std::ostream& operator<<(std::ostream& os, const Order& ord) { return os << ord.order_id; } }; int main() { const std::vector<int> v1{1, 2, 5, 5, 5, 9}; const std::vector<int> v2{2, 5, 7}; std::vector<int> diff; std::set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), std::inserter(diff, diff.begin())); std::cout << v1 << " ∖ " << v2 << " == " << diff << "\n\n"; // we want to know which orders "cut" between old and new states: std::vector<Order> old_orders{{1}, {2}, {5}, {9}}; std::vector<Order> new_orders{{2}, {5}, {7}}; std::vector<Order> cut_orders; std::set_difference(old_orders.begin(), old_orders.end(), new_orders.begin(), new_orders.end(), std::back_inserter(cut_orders), [](auto& a, auto& b) { return a.order_id < b.order_id; }); std::cout << "old orders: " << old_orders << '\n' << "new orders: " << new_orders << '\n' << "cut orders: " << 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}[edit] 不具合報告
以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。
| DR | 適用対象 | 公開された動作 | 正しい動作 |
|---|---|---|---|
| LWG 291 | C++98 | 入力範囲における等価な要素の扱いが未指定でした。 | 指定された |
[edit] 関連項目
| あるシーケンスが別のシーケンスの部分シーケンスである場合に true を返す (関数テンプレート) | |
| 2つの集合の対称差を計算する (関数テンプレート) | |
| (C++20) |
2つの集合の差を計算する (アルゴリズム関数オブジェクト) |