Algorithm Find If Not Function - C++ Programming Tutorial
C++ Course / STL Algorithm / Algorithm Find If Not Function

Algorithm Find If Not Function

BLUF: Mastering Algorithm Find If Not 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 Find If Not Function

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

The findifnot function in C++ algorithms retrieves the first element within a range where the predicate value is untrue; otherwise, it returns the last element in the range.

Syntax

Example

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

Parameter

It designates the initial element within the specified range.

The final value: It denotes the concluding element within the specified range.

It is commonly a single-input function where the output values are examined to produce a true or false result.

Return value

The function provides an iterator pointing to the initial element within the range where the predicate value evaluates to false. In cases where no such element exists, the function will return the final element instead.

Example 1

Example

#include<iostream>
#include<algorithm>
#include<array>
int main()
{
	std::array<int,6> a={6,7,8,9,10};
	std::array<int,6>::iterator ti=std::find_if_not (a.begin(), a.end(), [](int k){return k%2;} );
	std::cout<<"In the range given the very first even value is "<<*ti<<"\n";
	return 0;
}

Output:

Output

In the range given the very first even value is 6

Example 2

Example

#include<iostream>
#include<algorithm>
#include<vector>
bool isEven (int i)
{
	return((i%2)==0);
}
int main()
{
	std::vector<int> newvector {20, 35, 50, 65};
	std::vector<int>::iterator ti;
	ti= std::find_if(newvector.begin(),newvector.end(),isEven);
	std::cout<<"Out of the given elements, first even element is  "<<*ti<<"\n";
	std::vector<int>::iterator tie;
	tie=std::find_if_not(newvector.begin(), newvector.end(), isEven);
	std::cout<<"Out of the given elements, first odd element is "<<*tie<<"\n";
	return 0;

}

Output:

Output

Out of the given elements, first odd element is 20
Out of the given elements, first odd element is 35

Complexity

The process progresses sequentially, commencing from the initial item and advancing towards the final one. Every item in the list undergoes a validation 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 its 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