TheDeveloperBlog.com

Home | Contact Us

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

C++ algorithm for_each() function

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

C++ Algorithm for_each() function applies the function func to all of the elements in the range from 'first' to 'last'.

Syntax

template <class InputIterator, class Function>
Function for_each (InputIterator first, InputIterator last, Function func);

Parameter

first: It specifies the first element in the list.

last: It specifies the last element in the list.

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

Return value

The function returns 'func'.

Example 1

#include <iostream>
#include <algorithm>
#include <vector>
void newfunction (int k)
{
	std::cout << " " <<k;
}
struct newclass
{
	void operator () (int k)
	{
		std::cout <<" "<<k;
	} 
}
newobject;
int main()
{
	std::vector<int> newvector;
	newvector.push_back(50);
	newvector.push_back(100);
	newvector.push_back(150);
	std::cout << "newvector contains:\n";
	for_each (newvector.begin () , newvector.end (), newfunction);
	std::cout<< "\n newvector contains:\n";
	for_each (newvector.begin (), newvector.end(), newfunction);
	std::cout<<"\n";
	return 0;
}

Output:

newvector contains: 50 100 150
newvector contains: 50 100 150

Example 2

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
void printx1(int b)
{
    cout << b * 2 << " ";
}
struct Class1
{
    void operator() (int b)
    {
        cout << b * 3 << " ";
    }
} obj1;
int main()
{
    int ar[5] = { 6, 7, 8, 9, 10 };
      cout << "Using Arrays:" << endl;
    cout << "Multiple of 2 of elements are : ";
    for_each(ar, ar + 5, printx1);
     cout << endl;
    cout << "Multiple of 3 of elements are : ";
    for_each(ar, ar + 5, obj1);
    cout << endl;
    vector<int> ar1 = { 2,3,5,7,1 };
     cout << "Using Vectors:" << endl;
    cout << "Multiple of 2 of elements are : ";
    for_each(ar1.begin(), ar1.end(), printx1);
    cout << endl;
    cout << "Multiple of 3 of elements are : ";
    for_each(ar1.begin(), ar1.end(), obj1);
    cout << endl;
     }

Output:

Using Arrays:                                                                                                                  
Multiple of 2 of elements are : 12 14 16 18 20                                                                                 
Multiple of 3 of elements are : 18 21 24 27 30                                                                                 
Using Vectors:                                                                                                                 
Multiple of 2 of elements are : 4 6 10 14 2                                                                                    
Multiple of 3 of elements are : 6 9 15 21 3

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