Facebook Page Twitter Page LinkedIn Page
× C++ Standard Library C++ STL Library


The C++ algorithm::none_of function returns true if pred returns false for all elements in the range [first,last) or if the range is empty, else returns false.

Syntax

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

Parameters

first Specify initial position of the input iterator. The range used is [first,last).
last Specify final position of the input iterator. The range used is [first,last).
pred Specify an unary function that accepts an element in the range as argument, and returns a value convertible to bool. The function returns true if the element satisfy the given condition.

Return Value

Returns true if pred returns false for all elements in the range or if the range is empty, else returns false.

Time Complexity

Up to Linear i.e, Θ(n).

Example:

In the below example, the algorithm::none_of function is used to check whether none element of a given sequence satisfy the specified condition or not.

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

bool isNegative (int i) {
  return (i < 0) ;
}

int main (){
  int arr[] = {10, 15, 12, -15, 14, -12};
  vector<int> vec = {10, 15, 12, 15, 14, 12};

  //check if no element in the array is negative
  bool b1 = none_of(arr, arr+6, isNegative);

  //check if no element in the vector is negative
  bool b2 = none_of(vec.begin(), vec.end(), isNegative);

  //print result for array
  if(b1) {
    cout<<"Array contains no negative element.\n";
  }
  else {
    cout<<"Array contains negative elements.\n";
  }

  //print result for vector
  if(b2) {
    cout<<"Vector contains no negative element.\n";
  }
  else {
    cout<<"Vector contains negative elements.\n";
  }

  return 0;
}

The output of the above code will be:

Array contains negative elements.
Vector contains no negative element.

❮ C++ - Algorithm