C++ 算法 中的 none_of函数

首页 / C++入门教程 / C++ 算法 中的 none_of函数

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

none_of - 语法

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

none_of - 参数

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

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

pred  - 这是一元函数,它接受参数内的参数。

none_of - 返回值

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

none_of - 例子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

none_of - 例子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

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

深入剖析Kubernetes -〔张磊〕

即时消息技术剖析与实战 -〔袁武林〕

DDD实战课 -〔欧创新〕

正则表达式入门课 -〔涂伟忠〕

重学线性代数 -〔朱维刚〕

手把手教你玩音乐 -〔邓柯〕

手把手带你写一门编程语言 -〔宫文学〕

大数据经典论文解读 -〔徐文浩〕

Go进阶 · 分布式爬虫实战 -〔郑建勋〕

好记忆不如烂笔头。留下您的足迹吧 :)