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
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
#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:
In the range given the very first even value is 6
Example 2
#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:
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.