all_of 源码剖析

来源:互联网 发布:sql 逐行累加 编辑:程序博客网 时间:2024/05/29 12:21
一:用法解析

函数原型:

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

功能:

判断[ first , last )范围内的元素是否都满足pred,若是,返回真;其他情况,返回假。

例子:

// all_of example#include <iostream>     // std::cout#include <algorithm>    // std::all_of#include <array>        // std::arrayint 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;}
运行如下:

All the elements are odd numbers.


二:源码剖析
// TEMPLATE FUNCTION all_oftemplate<class _InIt,    class _Pr> inline    bool _All_of(_InIt _First, _InIt _Last, _Pr _Pred)    {   // test if all elements satisfy _Pred    for (; _First != _Last; ++_First)        if (!_Pred(*_First))            return (false);    return (true);    }template<class _InIt,    class _Pr> inline    bool all_of(_InIt _First, _InIt _Last, _Pr _Pred)    {   // test if all elements satisfy _Pred    _DEBUG_RANGE_PTR(_First, _Last, _Pred);    return (_All_of(_Unchecked(_First), _Unchecked(_Last), _Pred));    }





源码摘抄自Visual Studio 2015安装目录algorithm文件中。



点击进入目录----> C++源码剖析目录









1 0