The C++ none_of function in algorithms will return true if the predicate 'pred' evaluates to false. This should hold true for every element within the range [first, last).
Syntax
template <class InputIterator, class UnaryPredicate>
bool none_of (InputIterator first, InputIterator last, UnaryPredicate pred);
Parameter
It designates the initial element within the list.
last : It specifies the last element in the list.
The "pred" function is a unary function that takes an argument from the specified range.
Return value
The function will return 'true' as the return type if all elements within the range evaluate to 'false' when passed through the 'pred' argument; otherwise, it will return 'false'.
Example 1
#include <iostream>
#include <algorithm>
#include <array>
int main()
{
std::array<int, 6> arr= {25,27,29,31,33,35};
if ( std::none_of(arr.begin(), arr.end(), [](int k) {return k%2==0;} ) )
std::cout <<"None of the elements is divisible by 2";
return 0;
}
Output:
None of the elements is divisible by 2
Example 2
#include<iostream>
#include<algorithm>
using namespace std;
bool abc(int b)
{
return b<0;
}
int main()
{
int ar[] = { 2,4,6,8,12,0 };
int p = sizeof(ar)/sizeof(ar[0]);
cout<<"Array";
for(int k=0; k<p; k++)
cout<<" "<<ar[k];
if(none_of(ar, ar+p, abc))
cout<<"None of the elements in the range are negative";
else
cout<<"The range has at least one element that is negative";
return 0;
}
Output:
Array 2 4 6 8 12None of the elements in the range are negative
Complexity
The process progresses sequentially, beginning at the initial element and moving towards the final one. Each item in the list is evaluated against the 'pred' value. The iteration continues until a discrepancy in the 'pred' value is identified.
Data races
The function accesses either all the objects within the specified range or a subset of them.
Exceptions
The function will raise an exception if any of the arguments also raises an exception.