TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C++ algorithm find_if_not() function

C++ algorithm find_if_not() function tutorial for beginners and professionals with examples on adjacent_find(),any_of(), copy(), copy_if(), count(), count_if(), equal(), find(), find_end(), find_first_of(), find_if(), find_if_not(), for_each() etc.

<< Back to CPP

C++ Algorithm Function find_if_not()

C++ Algorithm find_if_not()function returns the value of the first element in the range for which the pred value is false otherwise the last element of the range is given.

Syntax

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

Parameter

first: It specifies the first element of the range.

last: It specifies the last element of the range.

pred: It is usually a unary function for which the range values are checked to return a boolean answer.

Return value

The function returns an iterator to the first element of the range for which the pred value is false. If no such element is found, then the function returns the last element.

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 function moves in a linear way, starting from the first element going towards the last one. For each element of the list value of 'pred' is checked. The search goes on until a mismatch for the 'pred' value is encountered.

Data races

Either all the objects in the specified range or some of them are accessed by the function.

Exceptions

The function throws an exception if any of the argument throws one.






Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf