📜  C++ STL-algorithm.none_of()函数

📅  最后修改于: 2020-10-16 14:08:23             🧑  作者: Mango

C++ STL algorithm函数none_of()

如果’pred’参数的值为false,则C++ STL algorithm.none_of()函数将返回true值。对于[first,last)范围内的所有元素,该值均应为false。

句法

template 
bool none_of (InputIterator first, InputIterator last, UnaryPredicate pred);

参数

first:它指定列表中的第一个元素。

last:它指定列表中的最后一个元素。

pred:这是一元函数,接受范围内的参数。

返回值

该函数具有一种返回类型“ true”。如果参数’pred’的值对于该范围的所有元素都是false,则返回值’true’,否则返回false。

例子1

#include 
#include 
#include 
int main()
{
    std::array arr= {25,27,29,31,33,35};
    if ( std::none_of(arr.begin(), arr.end(), [](int k) {return k%2==0;} ) )
    std::cout <<"None of the elements is divisible by 2";
    return 0;
}

输出:

 None of the elements is divisible by 2

例子2

#include
#include
using namespace std;
bool abc(int b)
{
    return b<0;
}
int main()
{
    int ar[] = { 2,4,6,8,12,0 };
    int p = sizeof(ar)/sizeof(ar[0]);
    cout<<"Array";
    for(int k=0; k

输出:

Array 2 4 6 8 12None of the elements in the range are negative

复杂度

该函数以线性方式移动,从第一个元素到最后一个元素。对于列表中的每个元素,都会检查“ pred”的值。搜索继续进行,直到遇到“ pred”值不匹配的情况。

数据竞争

函数可以访问指定范围内的所有对象,也可以访问其中的一些对象。

异常处理

如果任何参数抛出一个异常,该函数将引发异常。