TheDeveloperBlog.com

Home | Contact Us

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

C++ algorithm any_of() function

C++ algorithm any_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 Function any_of()

C++ Algorithm any_of() function tests the value of 'pred' for every element in the range, if for any element the value of pred is true, then the function returns true else return false.

Syntax

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

Parameter

first: It is the first element in the range specified.

last: It is the last element in the range.

pred: It is a 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 true for any of the elements of the range then the value 'true' is returned, else false.

Example 1

#include <iostream>
#include <algorithm>
#include <array>
using namespace std;
int main()
{
	int arr[7] = {2,4,6,5,10,3,14};
	any_of(arr,arr+6, [](int k){return k%2;})?
	cout <<"There are elements which exist in the table of 2":
	cout<<"No elements in the table of 2 exists";
	return 0;
}

Output:

There are elements which exist in the table of 2.

Example 2

#include <iostream>
#include <algorithm>
#include <array>
int main()
{
	std::array<int, 5> arr = {2,-4,6,-9,10};
	if(std::any_of (arr.begin(), arr.end(), [](int k) { return k<0;}))
	std::cout <<"Negative elements exist in the array";
	return 0;
}

Output:

Negative elements exist in the array

Complexity

The function moves linearly, starting from the first element going towards the last one. For each element of the list, the value of 'pred' is checked. The search goes on until a mismatch for the 'pred' value is encountered.

Data races

Either the function accesses all the objects in the specified range or some of them.

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