跳到主要内容

std::none_of() 算法

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

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

检查某个谓词是否对范围内的所有元素都不成立。

  • (1) 检查一元谓词 p 是否对范围 [first; last) 中的每个元素都返回 false。

  • (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
second

要检查的元素范围。

policy

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

p

一元谓词。

对于类型为(可能是 const)VT 的每个参数 v,表达式 p(v) 必须可转换为 bool,其中 VTInputIt 的值类型,无论值类别如何,并且不得修改 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

可能的实现

none_of (1)
template<class InputIt, class UnaryPredicate>
constexpr bool none_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::none_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::none_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::none_of() 算法

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

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

检查某个谓词是否对范围内的所有元素都不成立。

  • (1) 检查一元谓词 p 是否对范围 [first; last) 中的每个元素都返回 false。

  • (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
second

要检查的元素范围。

policy

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

p

一元谓词。

对于类型为(可能是 const)VT 的每个参数 v,表达式 p(v) 必须可转换为 bool,其中 VTInputIt 的值类型,无论值类别如何,并且不得修改 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

可能的实现

none_of (1)
template<class InputIt, class UnaryPredicate>
constexpr bool none_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::none_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::none_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 页面。它可能为了改进或编辑者偏好而进行了修改。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。