跳到主要内容

std::any_of() 算法

// (1)
template< class InputIt, class UnaryPredicate >
constexpr bool any_of( InputIt first, InputIt last, UnaryPredicate p );

// (2)
template< class ExecutionPolicy, class ForwardIt, class UnaryPredicate >
bool any_of( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last,
UnaryPredicate p );

检查范围内是否有任何元素满足谓词。

  • (1) 检查一元谓词 p 是否对范围 [first; last) 中的任何元素返回 true。

  • (2)(1) 相同,但根据 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

要使用的执行策略。有关详细信息,请参阅执行策略

r

一元谓词。

对于类型为 (可能是 const) VT 的每个参数 v(其中 VTInputIt 的值类型),表达式 p(v) 必须可转换为 bool,无论值类别如何,并且不得修改 v。因此,不允许使用参数类型 VT&,也不允许使用 VT,除非对于 VT,移动等同于复制。 (从 C++11 开始)

类型要求

InputItLegacyInputIterator
ForwardItLegacyForwardIterator
UnaryPredicatePredicate

返回值

如果一元谓词对范围内的任何元素返回 true,则为 true;否则为 false

重要

如果范围为空,则返回 true

复杂度

  • (1) 最多 last - first 次谓词应用。
  • (2) O(last - first) 次谓词应用。

异常

带有模板参数 ExecutionPolicy 的重载报告错误如下

  • 如果作为算法一部分调用的函数执行抛出异常,并且 ExecutionPolicy标准策略之一,则调用 std::terminate。对于任何其他 ExecutionPolicy,行为是实现定义的.
  • 如果算法未能分配内存,则抛出 std::bad_alloc

可能的实现

any_of (1)
template<class InputIt, class UnaryPredicate>
constexpr bool any_of(InputIt first, InputIt last, UnaryPredicate p)
{
return std::find_if(first, last, p) != last;
}

示例

Main.cpp
#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::any_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
本文源自 此 CppReference 页面。它可能已被修改以进行改进或满足编辑偏好。点击“编辑此页面”查看本文档所做的所有更改。
悬停查看原始许可证。

std::any_of() 算法

// (1)
template< class InputIt, class UnaryPredicate >
constexpr bool any_of( InputIt first, InputIt last, UnaryPredicate p );

// (2)
template< class ExecutionPolicy, class ForwardIt, class UnaryPredicate >
bool any_of( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last,
UnaryPredicate p );

检查范围内是否有任何元素满足谓词。

  • (1) 检查一元谓词 p 是否对范围 [first; last) 中的任何元素返回 true。

  • (2)(1) 相同,但根据 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

要使用的执行策略。有关详细信息,请参阅执行策略

r

一元谓词。

对于类型为 (可能是 const) VT 的每个参数 v(其中 VTInputIt 的值类型),表达式 p(v) 必须可转换为 bool,无论值类别如何,并且不得修改 v。因此,不允许使用参数类型 VT&,也不允许使用 VT,除非对于 VT,移动等同于复制。 (从 C++11 开始)

类型要求

InputItLegacyInputIterator
ForwardItLegacyForwardIterator
UnaryPredicatePredicate

返回值

如果一元谓词对范围内的任何元素返回 true,则为 true;否则为 false

重要

如果范围为空,则返回 true

复杂度

  • (1) 最多 last - first 次谓词应用。
  • (2) O(last - first) 次谓词应用。

异常

带有模板参数 ExecutionPolicy 的重载报告错误如下

  • 如果作为算法一部分调用的函数执行抛出异常,并且 ExecutionPolicy标准策略之一,则调用 std::terminate。对于任何其他 ExecutionPolicy,行为是实现定义的.
  • 如果算法未能分配内存,则抛出 std::bad_alloc

可能的实现

any_of (1)
template<class InputIt, class UnaryPredicate>
constexpr bool any_of(InputIt first, InputIt last, UnaryPredicate p)
{
return std::find_if(first, last, p) != last;
}

示例

Main.cpp
#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::any_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
本文源自 此 CppReference 页面。它可能已被修改以进行改进或满足编辑偏好。点击“编辑此页面”查看本文档所做的所有更改。
悬停查看原始许可证。