C++ 算法 中的 all_of函数

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

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

all_of - 语法

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

all_of - 参数

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

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

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

all_of - 返回值

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

all_of - 例子1

#include<iostream>
#include<algorithm>
#include<array>
int main()
{
	std::array<int, 6> arr= {25,27,29,31,33,35};
	if ( std::all_of(arr.begin(), arr.end(), [](int k) {return k%2;} ) )
	std::cout <<"All the array elements are odd.";
	return 0;
}

输出:

All the array elements are odd.

all_of - 例子2

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	int ar[6] = {2, 5, -7, -9, 3, 5};
	all_of(ar, ar+6, [](int x) { return x>0; })?
	cout<<"All elements are positive \n":
	cout<<"All elements are not positive";
	return 0;
}

输出:

All elements are not positive 

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

技术教程推荐

推荐系统三十六式 -〔刑无刀〕

TensorFlow快速入门与实战 -〔彭靖田〕

Kafka核心技术与实战 -〔胡夕〕

深入浅出云计算 -〔何恺铎〕

视觉笔记入门课 -〔高伟〕

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

WebAssembly入门课 -〔于航〕

人人都用得上的写作课 -〔涵柏〕

说透5G -〔杨四昌〕

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