名前空間
変種
操作

std::includes

提供: cppreference.com
< cpp‎ | algorithm
 
 
アルゴリズムライブラリ
制約付きアルゴリズムと範囲に対するアルゴリズム (C++20)
コンセプトとユーティリティ: std::sortable, std::projected, ...
制約付きアルゴリズム: std::ranges::copy, std::ranges::sort, ...
実行ポリシー (C++17)
非変更シーケンス操作
(C++11)(C++11)(C++11)
(C++17)
変更シーケンス操作
未初期化記憶域の操作
分割操作
ソート操作
二分探索操作
集合操作 (ソート済み範囲用)
includes

ヒープ操作
(C++11)
最小/最大演算
(C++11)
(C++17)

順列
数値演算
C のライブラリ
 
ヘッダ <algorithm> で定義
(1)
template<class InputIt1, class InputIt2 >

bool includes( InputIt1 first1, InputIt1 last1,

               InputIt2 first2, InputIt2 last2 );
(C++20未満)
template<class InputIt1, class InputIt2 >

constexprbool includes( InputIt1 first1, InputIt1 last1,

                         InputIt2 first2, InputIt2 last2 );
(C++20以上)
template<class ExecutionPolicy, class ForwardIt1, class ForwardIt2 >

bool includes( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1,

               ForwardIt2 first2, ForwardIt2 last2 );
(2) (C++17以上)
(3)
template<class InputIt1, class InputIt2, class Compare >

bool includes( InputIt1 first1, InputIt1 last1,

               InputIt2 first2, InputIt2 last2, Compare comp );
(C++20未満)
template<class InputIt1, class InputIt2, class Compare >

constexprbool includes( InputIt1 first1, InputIt1 last1,

                         InputIt2 first2, InputIt2 last2, Compare comp );
(C++20以上)
template<class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class Compare >

bool includes( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1,

               ForwardIt2 first2, ForwardIt2 last2, Compare comp );
(4) (C++17以上)

ソート済み範囲 [first2, last2) がソート済み範囲 [first1, last1)部分シーケンスであれば true を返します (部分シーケンスは連続的である必要はありません)。

1) 両方の範囲が operator< でソートされていなければなりません。
3) 両方の範囲が指定された比較関数 comp でソートされていなければなりません。
2,4)(1,3) と同じですが、 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& を持つ必要はありませんが、関数は渡されたオブジェクトを変更してはならず、 Type1 型および Type2 型 (およびそれらの const 修飾された型) のすべての値を値カテゴリに関わらず受理できなければなりません (そのため Type1 & は許されません。 また Type1 に対してムーブがコピーと同等でなければ Type1 も許されません(C++11以上))。
Type1 および Type2 は、どちらも InputIt 型のオブジェクトの逆参照から暗黙に変換可能なものでなければなりません。 ​

型の要件
-
InputIt1, InputIt2LegacyInputIterator の要件を満たさなければなりません。
-
ForwardIt1, ForwardIt2LegacyForwardIterator の要件を満たさなければなりません。

[編集]戻り値

[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)returnfalse;if(!(*first1 <*first2))++first2;}returntrue;}
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))returnfalse;if(!comp(*first1, *first2))++first2;}returntrue;}

[編集]

#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つの集合の差を計算します
(関数テンプレート)[edit]
指定範囲の要素に対して検索を行います
(関数テンプレート)[edit]
close