C++教程
C++控制语句
C++函数
C++数组
C++指针
C++对象
C++继承
C++多态
C++抽象
C++常用
C++ STL教程
C++迭代器
C++程序

C++ none_of()

C++ 算法函数none_of()

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

语法

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

参数

first: 指定列表中的第一个元素。
last : 它指定列表中的last元素。
pred : 这是一元函数,接受范围内的参数。

返回值

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

示例1

#include <iostream>
#include <algorithm>
#include <array>
int main()
{
    std::array<int, 6> 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<iostream>
#include<algorithm>
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<p; k++)
    cout<<" "<<ar[k];
    if(none_of(ar, ar+p, abc))
    cout<<"None of the elements in the range are negative";
    else
    cout<<"The range has at least one element that is negative";
    return 0;
}
输出:
Array 2 4 6 8 12None of the elements in the range are negative

复杂度

该函数以线性方式移动,从第一个元素开始到last元素。对于列表中的每个元素,都会检查" pred"的值。搜索继续进行,直到遇到与" pred"值不匹配的地方。

数据竞争

指定范围内的所有对象或其中一些对象都是

异常

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

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4