作用: Test condition on all elements in range

Returns true if pred returns true for all the elements in the range [first,last) or if the range is empty, and false otherwise. 对所有元素进行条件判断, 若都成立则返回true, 有一个不成立就返回false, 元素为空时会返回true。

时间复杂度:

应用:

int nums[5] = {1,2,3,4,5};
if(all_of(nums, nums + 5, [](int i){return i%2;}))
	cout << "都是奇数\n";

函数原型

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