首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::any_of

Defined in header <algorithm>

?

?

template< class InputIt, class UnaryPredicate > bool all_of( InputIt first, InputIt last, UnaryPredicate p );

(1)

(since C++11)

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

(2)

(since C++17)

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

(3)

(since C++11)

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

(4)

(since C++17)

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

(5)

(since C++11)

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

(6)

(since C++17)

1%29检查是否为一元谓词p回报true对于范围内的所有元素[first, last)...

3%29检查是否为一元谓词p回报true范围内至少有一个元素[first, last)...

5%29检查是否为一元谓词p回报true对于范围内的任何元素[first, last)...

2,4,6%29等于%281,3,5%29,但根据policy这些过载不参与过载解决,除非std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>是真的

参数

first, last

-

the range of elements to examine

policy

-

the execution policy to use. See execution policy for details.

p

-

unary predicate . The signature of the predicate function should be equivalent to the following: bool pred(const Type &a); The signature does not need to have const &, but the function must not modify the objects passed to it. The type Type must be such that an object of type InputIt can be dereferenced and then implicitly converted to Type. ?

类型要求

-输入必须符合输入器的要求。

---。

-单数预测必须满足谓词的要求。

返回值

1-2%29true如果一元谓词返回true对于范围内的所有元素,false否则。回报true如果范围是空的。

3-4%29true如果一元谓词返回true在范围内至少有一个元素,false否则。回报false如果范围是空的。

5-6%29true如果一元谓词返回true在范围内没有任何元素,false否则。回报true如果范围是空的。

复杂性

最多为1,3,5%29last---first谓词的应用

2,4,6%29O(last-first)谓词的应用

例外

带有名为ExecutionPolicy报告错误如下:

  • 如果执行作为算法一部分调用的函数,则引发异常ExecutionPolicy是其中之一标准政策,,,std::terminate叫做。对于任何其他人ExecutionPolicy,行为是由实现定义的。
  • 如果算法不能分配内存,std::bad_alloc被扔了。

可能的实施

第一版

*。

模板<类Inputit,类UnaryPredicate>boolAll[医]%28 InputIt First,InputIt Lest,UnaryPredicate p%29{返回std::find[医]如果[医]不是%281,最后,p%29=最后;}

第二版

模板<类Inputit,类UnaryPredicate>boolany[医]%28 InputIt First,InputIt Lest,UnaryPredicate p%29{返回std::find[医]如果是%281,则为最后,p%29%21=最后;}

第三版

模板<类Inputit,类UnaryPredicate>boolno[医]%28 InputIt First,InputIt Lest,UnaryPredicate p%29{返回std::find[医]如果%281,最后,p%29=最后;}

二次

代码语言:javascript
复制
#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
    {
        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";
    }
}

二次

产出:

二次

代码语言:javascript
复制
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

二次

另见

二次

*。

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com