Algorithm None Of Function - C++ Programming Tutorial
C++ Course / STL Algorithm / Algorithm None Of Function

Algorithm None Of Function

BLUF: Mastering Algorithm None Of Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Algorithm None Of Function

C++ is renowned for its efficiency. Learn how Algorithm None Of Function enables low-level control and high-performance computing in the tutorial below.

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

Example

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

Example

#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:

Output

None of the elements is divisible by 2

Example 2

Example

#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:

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.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience