Home C&C++函数库 c++ 语法 程序源码 Linux C库

std::

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;
}

☲  参数


first, last
指向序列初始和最终位置的输入迭代器。使用的范围是[first,last], 它包含first和last之间的所有元素,包括first指向的元素,但不包括last指向的元素。
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;
}

输出:
There are no negative elements in the range.

✥ 复杂度



在first 和last之间的distance达到线性:对每个元素调用pred,直到发现匹配。

⇄ 数据竞争


[first,last)范围内的部分(或全部)对象被访问(最多一次)。

☂ 异常安全性



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

🍄  另请参阅



all_of 测试范围内所有元素的情况(函数模板)
any_of 测试范围内是否有满足条件的元素(函数模板)
find_if 在范围内查找元素(函数模板)

联系我们 免责声明 关于CandCplus 网站地图