The Wayback Machine - https://web.archive.org/web/20180601204956/http://ja.cppreference.com:80/w/cpp/algorithm/all_any_none_of
名前空間
変種
操作

std::all_of, std::any_of, std::none_of

提供: cppreference.com
< cpp‎ | algorithm
 
 
アルゴリズムライブラリ
実行ポリシー (C++17)
非変更シーケンス操作
all_ofany_ofnone_of
(C++11)(C++11)(C++11)
(C++17)
変更シーケンス操作
未初期化記憶域の操作
分割操作
ソート操作
バイナリサーチ操作
集合操作 (ソート済み範囲用)
ヒープ操作
(C++11)
最小/最大演算
(C++11)
(C++17)
順列
数値演算
C のライブラリ
 
ヘッダ <algorithm> で定義
(1)
template<class InputIt, class UnaryPredicate >
bool all_of( InputIt first, InputIt last, UnaryPredicate p );
(C++11およびそれ以降)
(C++20以前)
template<class InputIt, class UnaryPredicate >
constexprbool all_of( InputIt first, InputIt last, UnaryPredicate p );
(C++20およびそれ以降)
template<class ExecutionPolicy, class ForwardIt, class UnaryPredicate >
bool all_of( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, UnaryPredicate p );
(2) (C++17およびそれ以降)
(3)
template<class InputIt, class UnaryPredicate >
bool any_of( InputIt first, InputIt last, UnaryPredicate p );
(C++11およびそれ以降)
(C++20以前)
template<class InputIt, class UnaryPredicate >
constexprbool any_of( InputIt first, InputIt last, UnaryPredicate p );
(C++20およびそれ以降)
template<class ExecutionPolicy, class ForwardIt, class UnaryPredicate >
bool any_of( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, UnaryPredicate p );
(4) (C++17およびそれ以降)
(5)
template<class InputIt, class UnaryPredicate >
bool none_of( InputIt first, InputIt last, UnaryPredicate p );
(C++11およびそれ以降)
(C++20以前)
template<class InputIt, class UnaryPredicate >
constexprbool none_of( InputIt first, InputIt last, UnaryPredicate p );
(C++20およびそれ以降)
template<class ExecutionPolicy, class ForwardIt, class UnaryPredicate >
bool none_of( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, UnaryPredicate p );
(6) (C++17およびそれ以降)
1) 範囲 [first, last) 内のすべての要素に対して単項述語 ptrue を返すかどうかを調べます。
3) 範囲 [first, last) 内の少なくともひとつの要素に対して単項述語 ptrue を返すかどうかを調べます。
5) 範囲 [first, last) 内のいずれの要素に対しても単項述語 ptrue を返さないかどうかを調べます。
2,4,6)(1,3,5) と同じですが、 policy に従って実行されます。 これらのオーバーロードは、 std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> が true でなければ、オーバーロード解決に参加しません。

目次

[編集]引数

first, last - 調べる要素の範囲
policy - 使用する実行ポリシー。 詳細は実行ポリシーを参照してください
p - 単項述語。

述語関数のシグネチャは以下と同等なものであるべきです。

 bool pred(const Type &a);

シグネチャが const& を持つ必要はありませんが、関数は渡されたオブジェクトを変更してはなりません。
TypeInputIt 型のオブジェクトの逆参照から暗黙に変換可能なものでなければなりません。 ​

型の要件
-
InputItInputIterator の要件を満たさなければなりません。
-
ForwardItForwardIterator の要件を満たさなければなりません。
-
UnaryPredicatePredicate の要件を満たさなければなりません。

[編集]戻り値

1-2) 範囲内のすべての要素に対して単項述語が true を返す場合は true、そうでなければ false。 範囲が空の場合は true を返します。
3-4) 範囲内の少なくともひとつの要素に対して単項述語が true を返す場合は true、そうでなければ false。 範囲が空の場合は false を返します。
5-6) 範囲内のいずれの要素に対しても単項述語が true を返さない場合は true、そうでなければ false。 範囲が空の場合は true を返します。

[編集]計算量

1,3,5) 多くとも last - first 回の述語の適用。
2,4,6)O(last-first) 回の述語の適用。

[編集]例外

テンプレート引数 ExecutionPolicy を持つオーバーロードは以下のようにエラーを報告します。

  • アルゴリズムの一部として呼び出された関数の実行が例外を投げ、 ExecutionPolicy が3つの標準のポリシーのいずれかの場合は、 std::terminate が呼ばれます。 それ以外のあらゆる ExecutionPolicy については、動作は処理系定義です。
  • アルゴリズムがメモリの確保に失敗した場合は、 std::bad_alloc が投げられます。

[編集]実装例

1つめのバージョン
template<class InputIt, class UnaryPredicate >bool all_of(InputIt first, InputIt last, UnaryPredicate p){returnstd::find_if_not(first, last, p)== last;}
2つめのバージョン
template<class InputIt, class UnaryPredicate >bool any_of(InputIt first, InputIt last, UnaryPredicate p){returnstd::find_if(first, last, p)!= last;}
3つめのバージョン
template<class InputIt, class UnaryPredicate >bool none_of(InputIt first, InputIt last, UnaryPredicate p){returnstd::find_if(first, last, p)== last;}

[編集]

#include <vector>#include <numeric>#include <algorithm>#include <iterator>#include <iostream>#include <functional>   int main(){std::vector<int> v(10, 2);std::partial_sum(v.cbegin(), v.cend(), v.begin());std::cout<<"Among the numbers: ";std::copy(v.cbegin(), v.cend(), std::ostream_iterator<int>(std::cout, " "));std::cout<<'\n';   if(std::all_of(v.cbegin(), v.cend(), [](int i){return i %2==0;})){std::cout<<"All numbers are even\n";}if(std::none_of(v.cbegin(), v.cend(), std::bind(std::modulus<int>(), std::placeholders::_1, 2))){std::cout<<"None of them are odd\n";}struct DivisibleBy {constint d; DivisibleBy(int n): d(n){}bool operator()(int n)const{return n % d ==0;}};   if(std::any_of(v.cbegin(), v.cend(), DivisibleBy(7))){std::cout<<"At least one number is divisible by 7\n";}}

出力:

Among the numbers: 2 4 6 8 10 12 14 16 18 20 All numbers are even None of them are odd At least one number is divisible by 7

[編集]関連項目

close