TheDeveloperBlog.com

Home | Contact Us

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

C++ algorithm none_of() function

C++ algorithm none_of() 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 Functions none_of()

C++ Algorithm none_of() function returns a true value if the value of 'pred' argument is false. The value should be false for all elements in the range [first, last).

Syntax

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

Parameter

first : It specifies the first element in the list.

last: It specifies the last element in the list.

pred: It is an unary function which accepts the argument from the range.

Return value

The function has one return type, 'true'. If the value of argument 'pred' is false for all the elements of the range then the value 'true' is returned, else 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 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