名稱空間
變體
操作

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

來自 cppreference.com
< cpp‎ | 演算法
 
 
演算法庫
有約束演算法與針對範圍的演算法 (C++20)
有約束的演算法,例如 ranges::copyranges::sort 等……
執行策略 (C++17)
排序及相關操作
劃分操作
排序操作
二分搜尋操作
(於已劃分範圍上)
集合操作(於已排序範圍上)
歸併操作(於已排序範圍上)
堆操作
最小/最大值操作
(C++11)
(C++17)
字典序比較操作
排列操作
C 庫
數值操作
未初始化記憶體上的操作
 
定義於標頭檔案 <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,

             ForwardIt first, ForwardIt last, UnaryPred p );
(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,

             ForwardIt first, ForwardIt last, UnaryPred p );
(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,

              ForwardIt first, ForwardIt last, UnaryPred p );
(6) (C++17 起)
1) 檢查一元謂詞 p 對於範圍 [firstlast) 中至少一個元素是否返回 false
3) 檢查一元謂詞 p 對於範圍 [firstlast) 中至少一個元素是否返回 true
5) 檢查一元謂詞 p 對於範圍 [firstlast) 中所有元素是否均不返回 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) VT 的每個引數 v,表示式 p(v) 必須可轉換為 bool,其中 VTInputIt 的值型別,無論其值類別如何,並且不得修改 v。因此,不允許引數型別為 VT&,除非對於 VT 移動等同於複製,否則也不允許 VT(C++11 起)

型別要求
-
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
  1. 在此情況下,範圍為空。

[編輯] 複雜度

1-6) 最多 std::distance(first, last) 次謂詞 p 的應用。

[編輯] 異常

帶有模板引數 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

[編輯] 另請參閱

檢查範圍中的所有、任意一個或沒有元素使謂詞為 true
(演算法函式物件)[編輯]