std::includes
| ヘッダー <algorithm> で定義 |
||
template< class InputIt1, class InputIt2 > bool includes( InputIt1 first1, InputIt1 last1, |
(1) | (C++20 以降 constexpr) |
| template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 > |
(2) | (C++17以降) |
template< class InputIt1, class InputIt2, class Compare > bool includes( InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, Compare comp ); |
(3) | (C++20 以降 constexpr) |
| template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class Compare > |
(4) | (C++17以降) |
ソート済みの範囲 [first1, last1) におけるソート済みの範囲 [first2, last2) の部分列(部分列は連続である必要はありません)である場合、true を返します。
[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以降) |
目次 |
[edit] Parameters
| first1, last1 | - | 検査する要素のソート済み 範囲 を定義するイテレーターのペア |
| first2, last2 | - | 検索する要素のソート済み 範囲 を定義するイテレーターのペア |
| policy | - | 使用する 実行ポリシー |
| comp | - | 比較関数オブジェクト(つまり、Compare の要件を満たすオブジェクト)。最初の引数が2番目の引数より小さい(つまり、前に順序付けられる)場合に true を返します。 比較関数のシグネチャは、以下と同等でなければならない。 bool cmp(const Type1& a, const Type2& b); シグネチャは const& を持つ必要はありませんが、関数は渡されたオブジェクトを変更してはならず、値カテゴリ に関係なく、(おそらく const の) |
| 型要件 | ||
-InputIt1, InputIt2 は LegacyInputIterator の要件を満たす必要がある。 | ||
-ForwardIt1, ForwardIt2 は LegacyForwardIterator の要件を満たさなければなりません。 | ||
-CompareはCompareの要件を満たす必要がある。 | ||
[edit] Return value
[first2, last2) が [first1, last1) の部分列である場合は true、そうでない場合は false。
空のシーケンスは任意のシーケンスの部分列であるため、[first2, last2) が空の場合、true が返されます。
[edit] Complexity
std::distance(first1, last1) を N1 とし、std::distance(first2, last2) を N2 とする。
[edit] Exceptions
ExecutionPolicy というテンプレートパラメータを持つオーバーロードは、次のようにエラーを報告します。
- アルゴリズムの一部として呼び出された関数の実行が例外をスローし、
ExecutionPolicyが 標準ポリシー のいずれかである場合、std::terminate が呼び出されます。その他のExecutionPolicyの場合、動作は実装定義です。 - アルゴリズムがメモリの割り当てに失敗した場合、std::bad_alloc がスローされます。
[edit] Possible implementation
| include (1) |
|---|
template<class InputIt1, class InputIt2> bool includes(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2) { for (; first2 != last2; ++first1) { if (first1 == last1 || *first2 < *first1) return false; if (!(*first1 < *first2)) ++first2; } return true; } |
| include (3) |
template<class InputIt1, class InputIt2, class Compare> bool includes(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, Compare comp) { for (; first2 != last2; ++first1) { if (first1 == last1 || comp(*first2, *first1)) return false; if (!comp(*first1, *first2)) ++first2; } return true; } |
[edit] Example
#include <algorithm> #include <cctype> #include <iostream> template<class Os, class Co> Os& operator<<(Os& os, const Co& v) { for (const auto& i : v) os << i << ' '; return os << '\t'; } int main() { const auto v1 = {'a', 'b', 'c', 'f', 'h', 'x'}, v2 = {'a', 'b', 'c'}, v3 = {'a', 'c'}, v4 = {'a', 'a', 'b'}, v5 = {'g'}, v6 = {'a', 'c', 'g'}, v7 = {'A', 'B', 'C'}; auto no_case = [](char a, char b) { return std::tolower(a) < std::tolower(b); }; std::cout << v1 << "\nincludes:\n" << std::boolalpha << v2 << ": " << std::includes(v1.begin(), v1.end(), v2.begin(), v2.end()) << '\n' << v3 << ": " << std::includes(v1.begin(), v1.end(), v3.begin(), v3.end()) << '\n' << v4 << ": " << std::includes(v1.begin(), v1.end(), v4.begin(), v4.end()) << '\n' << v5 << ": " << std::includes(v1.begin(), v1.end(), v5.begin(), v5.end()) << '\n' << v6 << ": " << std::includes(v1.begin(), v1.end(), v6.begin(), v6.end()) << '\n' << v7 << ": " << std::includes(v1.begin(), v1.end(), v7.begin(), v7.end(), no_case) << " (case-insensitive)\n"; }
出力
a b c f h x includes: a b c : true a c : true a a b : false g : false a c g : false A B C : true (case-insensitive)
[edit] Defect reports
以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。
| DR | 適用対象 | 公開された動作 | 正しい動作 |
|---|---|---|---|
| LWG 1205 | C++98 | [first2, last2) が空の場合の戻り値は不明確でした。 |
この場合、true を返します。 |
[edit] See also
| 2つの集合の差を計算する (関数テンプレート) | |
| 要素の範囲の最初の出現を検索する (関数テンプレート) | |
| (C++20) |
あるシーケンスが別のシーケンスの部分シーケンスである場合に true を返す (アルゴリズム関数オブジェクト) |