std::includes
ヘッダ <algorithm> で定義
|
||
(1) | ||
template< class InputIt1, class InputIt2 > bool includes( InputIt1 first1, InputIt1 last1, |
(C++20未満) | |
template< class InputIt1, class InputIt2 > constexpr bool includes( InputIt1 first1, InputIt1 last1, |
(C++20以上) | |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 > bool includes( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1, |
(2) | (C++17以上) |
(3) | ||
template< class InputIt1, class InputIt2, class Compare > bool includes( InputIt1 first1, InputIt1 last1, |
(C++20未満) | |
template< class InputIt1, class InputIt2, class Compare > constexpr bool includes( InputIt1 first1, InputIt1 last1, |
(C++20以上) | |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class Compare > bool includes( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1, |
(4) | (C++17以上) |
ソート済み範囲 [first2, last2)
がソート済み範囲 [first1, last1)
の部分シーケンスであれば true を返します (部分シーケンスは連続的である必要はありません)。
comp
でソートされていなければなりません。policy
に従って実行されます。 このオーバーロードは、std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> が true である場合にのみ、オーバーロード解決に参加します。目次 |
[編集] 引数
first1, last1 | - | 調べる要素のソート済み範囲 |
first2, last2 | - | 検索する要素のソート済み範囲 |
policy | - | 使用する実行ポリシー。 詳細は実行ポリシーを参照してください |
comp | - | 最初の要素が2番目の要素より小さい (前に順序づけられる) 場合に true を返す、比較関数オブジェクト (Compare の要件を満たすオブジェクト)。 比較関数のシグネチャは以下と同等なものであるべきです。 bool cmp(const Type1 &a, const Type2 &b); シグネチャが const & を持つ必要はありませんが、関数は渡されたオブジェクトを変更してはならず、 |
型の要件 | ||
-InputIt1, InputIt2 は LegacyInputIterator の要件を満たさなければなりません。
| ||
-ForwardIt1, ForwardIt2 は LegacyForwardIterator の要件を満たさなければなりません。
|
[編集] 戻り値
[first2, last2)
が [first1, last1)
の部分シーケンスであれば true、そうでなければ false。
[編集] 計算量
多くとも 2·(N1+N2-1) 回の比較、ただし N1 = std::distance(first1, last1) で N2 = std::distance(first2, last2) です。
[編集] 例外
テンプレート引数 ExecutionPolicy
を持つオーバーロードは以下のようにエラーを報告します。
- アルゴリズムの一部として呼び出された関数の実行が例外を投げ、
ExecutionPolicy
が標準のポリシーのいずれかの場合は、 std::terminate が呼ばれます。 それ以外のあらゆるExecutionPolicy
については、動作は処理系定義です。 - アルゴリズムがメモリの確保に失敗した場合は、 std::bad_alloc が投げられます。
[編集] 実装例
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; } |
2つめのバージョン |
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; } |
[編集] 例
#include <iostream> #include <algorithm> #include <cctype> #include <vector> int main() { std::vector<char> v1 {'a', 'b', 'c', 'f', 'h', 'x'}; std::vector<char> v2 {'a', 'b', 'c'}; std::vector<char> v3 {'a', 'c'}; std::vector<char> v4 {'g'}; std::vector<char> v5 {'a', 'c', 'g'}; for (auto i : v1) std::cout << i << ' '; std::cout << "\nincludes:\n" << std::boolalpha; for (auto i : v2) std::cout << i << ' '; std::cout << ": " << std::includes(v1.begin(), v1.end(), v2.begin(), v2.end()) << '\n'; for (auto i : v3) std::cout << i << ' '; std::cout << ": " << std::includes(v1.begin(), v1.end(), v3.begin(), v3.end()) << '\n'; for (auto i : v4) std::cout << i << ' '; std::cout << ": " << std::includes(v1.begin(), v1.end(), v4.begin(), v4.end()) << '\n'; for (auto i : v5) std::cout << i << ' '; std::cout << ": " << std::includes(v1.begin(), v1.end(), v5.begin(), v5.end()) << '\n'; auto cmp_nocase = [](char a, char b) { return std::tolower(a) < std::tolower(b); }; std::vector<char> v6 {'A', 'B', 'C'}; for (auto i : v6) std::cout << i << ' '; std::cout << ": (case-insensitive) " << std::includes(v1.begin(), v1.end(), v6.begin(), v6.end(), cmp_nocase) << '\n'; }
出力:
a b c f h x includes: a b c : true a c : true g : false a c g : false A B C : (case-insensitive) true
[編集] 関連項目
2つの集合の差を計算します (関数テンプレート) | |
指定範囲の要素に対して検索を行います (関数テンプレート) |