C++ (all_of;any_of;none_of)

本文深入探讨了C++标准模板库(STL)中的三个关键算法:all_of、any_of和none_of,详细解释了这些算法的功能、用法及示例代码,帮助读者理解如何在C++中高效地进行序列元素条件检查。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、all_of

头文件algorithm

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

测试范围内所有元素的条件

如果pred对范围[first,last]中的所有元素返回true或者范围为空,则返回true,否则返回false。

此函数模板的行为等效于:

template<class InputIterator, class UnaryPredicate>
  bool all_of (InputIterator first, InputIterator last, UnaryPredicate pred)
{
  while (first!=last) {
    if (!pred(*first)) return false;
    ++first;
  }
  return true;
}

参数

  1. first,last
    将迭代器输入到序列中的初始位置和最终位置。 使用的范围是[first,last),它包含first和last之间的所有元素,包括first指向的元素,但不包括last指向的元素。
  2. pred
    一元函数,接受范围内的元素作为参数,并返回可转换为bool的值。 返回值表示元素是否满足此函数检查的条件。

该函数不得修改其参数。这可以是函数指针或函数对象。

返回值

如果pred对范围内的所有元素返回true或者范围为空,则为true,否则为false。

// all_of example
#include <iostream>     // std::cout
#include <algorithm>    // std::all_of
#include <array>        // std::array

int main () {
	std::array<int,8> foo = {3,5,7,11,13,17,19,23};

	if ( std::all_of(foo.begin(), foo.end(), [](int i) {
	return i%2;
}) )
	std::cout << "All the elements are odd numbers.\n";

	return 0;
}

在这里插入图片描述

例外

如果pred或者迭代器上的操作抛出,则抛出。请注意,无效参数会导致未定义的行为。

二、any_of

头文件algorithm

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

测试范围内的任何元素是否满足条件

如果pred对范围[first,last]中的任何元素返回true,则返回true,否则返回false。

如果[first,last)是空范围,则该函数返回false。

此函数模板的行为等效于:

template<class InputIterator, class UnaryPredicate>
  bool any_of (InputIterator first, InputIterator last, UnaryPredicate pred)
{
  while (first!=last) {
    if (pred(*first)) return true;
    ++first;
  }
  return false;
}

参数

  1. first,last
    将迭代器输入到序列中的初始位置和最终位置。 使用的范围是[first,last),它包含first和last之间的所有元素,包括first指向的元素,但不包括last指向的元素。
  2. pred
    一元函数,接受范围内的元素作为参数,并返回可转换为bool的值。 返回值表示元素是否满足此函数检查的条件。

该函数不得修改其参数。这可以是函数指针或函数对象。

返回值

如果pred对范围[first,last]中的任何元素返回true,则返回true,否则返回false。如果[first,last)是空范围,则该函数返回false。

// any_of example
#include <iostream>     // std::cout
#include <algorithm>    // std::any_of
#include <array>        // std::array

int main () {
  std::array<int,7> foo = {0,1,-1,3,-3,5,-5};

  if ( std::any_of(foo.begin(), foo.end(), [](int i){return i<0;}) )
    std::cout << "There are negative elements in the range.\n";

  return 0;
}

在这里插入图片描述

三、none_of

头文件algorithm

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

测试没有元素满足条件

如果pred对范围[first,last]中的所有元素返回false或者范围为空,则返回true,否则返回false。

此函数模板的行为等效于:

template<class InputIterator, class UnaryPredicate>
  bool none_of (InputIterator first, InputIterator last, UnaryPredicate pred)
{
  while (first!=last) {
    if (pred(*first)) return false;
    ++first;
  }
  return true;
}

参数

  1. first,last
    将迭代器输入到序列中的初始位置和最终位置。 使用的范围是[first,last),它包含first和last之间的所有元素,包括first指向的元素,但不包括last指向的元素。
  2. pred
    一元函数,接受范围内的元素作为参数,并返回可转换为bool的值。 返回值表示元素是否满足此函数检查的条件。

该函数不得修改其参数。这可以是函数指针或函数对象。

返回值

如果pred对范围[first,last]中的所有元素返回false或者范围为空,则为true,否则为false。

// none_of example
#include <iostream>     // std::cout
#include <algorithm>    // std::none_of
#include <array>        // std::array

int main () {
  std::array<int,8> foo = {1,2,4,8,16,32,64,128};

  if ( std::none_of(foo.begin(), foo.end(), [](int i){return i<0;}) )
    std::cout << "There are no negative elements in the range.\n";

  return 0;
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值