std::all_of, std::any_of, std::none_of
來自 cppreference.com
定義於標頭檔案 <algorithm> |
||
template< class InputIt, class UnaryPred > bool all_of( InputIt first, InputIt last, UnaryPred p ); |
(1) | (C++11 起) (C++20 起為 constexpr) |
template< class ExecutionPolicy, class ForwardIt, class UnaryPred > bool all_of( ExecutionPolicy&& policy, |
(2) | (C++17 起) |
template< class InputIt, class UnaryPred > bool any_of( InputIt first, InputIt last, UnaryPred p ); |
(3) | (C++11 起) (C++20 起為 constexpr) |
template< class ExecutionPolicy, class ForwardIt, class UnaryPred > bool any_of( ExecutionPolicy&& policy, |
(4) | (C++17 起) |
template< class InputIt, class UnaryPred > bool none_of( InputIt first, InputIt last, UnaryPred p ); |
(5) | (C++11 起) (C++20 起為 constexpr) |
template< class ExecutionPolicy, class ForwardIt, class UnaryPred > bool none_of( ExecutionPolicy&& policy, |
(6) | (C++17 起) |
1) 檢查一元謂詞 p 對於範圍
[
first,
last)
中至少一個元素是否返回 false。3) 檢查一元謂詞 p 對於範圍
[
first,
last)
中至少一個元素是否返回 true。5) 檢查一元謂詞 p 對於範圍
[
first,
last)
中所有元素是否均不返回 true。2,4,6) 與 (1,3,5) 相同,但根據 policy 執行。
僅當滿足所有以下條件時,這些過載才參與過載決議
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 起) |
目錄 |
[編輯] 引數
first, last | - | 定義要檢查的元素範圍的迭代器對 |
policy | - | 要使用的 執行策略 |
p | - | 一元謂詞。 對於型別為 (可能為 const) |
型別要求 | ||
-InputIt 必須滿足 LegacyInputIterator 的要求。 | ||
-ForwardIt 必須滿足 LegacyForwardIterator 的要求。 | ||
-UnaryPred 必須滿足 Predicate 的要求。 |
[編輯] 返回值
包含 true 元素 | 是 | 否 | ||
---|---|---|---|---|
包含 false 元素 | 是 | 否 | 是 | 否[1] |
all_of
|
false | true | false | true |
any_of
|
true | true | false | false |
none_of
|
false | false | true | true |
- ↑ 在此情況下,範圍為空。
[編輯] 複雜度
[編輯] 異常
帶有模板引數 ExecutionPolicy
的過載按如下方式報告錯誤
- 若作為演算法一部分呼叫的函式執行丟擲異常,且
ExecutionPolicy
為標準策略之一,則呼叫 std::terminate。對於任何其他ExecutionPolicy
,行為是實現定義的。 - 如果演算法未能分配記憶體,則丟擲 std::bad_alloc。
[編輯] 可能的實現
另見
- libstdc++ 和 libc++ 中
all_of
的實現。 - libstdc++ 和 libc++ 中
any_of
的實現。 - libstdc++ 和 libc++ 中
none_of
的實現。
all_of |
---|
template<class InputIt, class UnaryPred> constexpr bool all_of(InputIt first, InputIt last, UnaryPred p) { return std::find_if_not(first, last, p) == last; } |
any_of |
template<class InputIt, class UnaryPred> constexpr bool any_of(InputIt first, InputIt last, UnaryPred p) { return std::find_if(first, last, p) != last; } |
none_of |
template<class InputIt, class UnaryPred> constexpr bool none_of(InputIt first, InputIt last, UnaryPred p) { return std::find_if(first, last, p) == last; } |
[編輯] 示例
執行此程式碼
#include <algorithm> #include <functional> #include <iostream> #include <iterator> #include <numeric> #include <vector> 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<>(), std::placeholders::_1, 2))) std::cout << "None of them are odd\n"; struct DivisibleBy { const int 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
[編輯] 另請參閱
(C++20)(C++20)(C++20) |
檢查範圍中的所有、任意一個或沒有元素使謂詞為 true (演算法函式物件) |