C++ Programming Code Examples
Learn C++ Language
Algorithm Library for_each() Function in C++ Programming Language
Algorithm Library for_each() Function in C++
Apply function to range. Applies function fn to each of the elements in the range [first,last).
C++ algorithm::for_each function is used to apply the specified function fn to each element in the range [first, last).
for_each loop applies a function to the range of elements of a collection. In other words each element from the collection will be passed to the function as a parameter and function will be executed the number of times elements present in a collection. This loop is defined in a algorithm header so have to include this header in your program to use for_each
Syntax for Algorithm for_each() Function in C++
#include <algorithm>
template <class InputIterator, class Function>
Function for_each (InputIterator first, InputIterator last, Function fn);
first, last
Input iterators to the initial and final positions in a sequence. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
fn
Unary function that accepts an element in the range as argument. This can either be a function pointer or a move constructible function object. Its return value, if any, is ignored.
Function returns fn, as if calling std::move(fn).
Complexity
Linear in the distance between first and last: Applies fn to each element.
Data races
The objects in the range [first,last) are accessed (each object is accessed exactly once).
These objects may be modified if InputIterator is a mutable iterator type and fn is not a constant function.
Exceptions
Throws if fn throws or if any of the operations on iterators throws. Note that invalid arguments cause undefined behavior.
/* The for_each() function is available in the <algorithm> header file in C++. It is used as a loop in the code to iterate and perform operations on each element. */
/* apply provided function on each element of the range by C++ std::algorithm::for_each() function code example. */
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
// helper function 1
void printx2(int a)
{
cout << a * 2 << " ";
}
// helper function 2
// object type function
struct Class2
{
void operator() (int a)
{
cout << a * 3 << " ";
}
} ob1;
int main()
{
// initializing array
int arr[5] = { 1, 5, 2, 4, 3 };
cout << "Using Arrays:" << endl;
// printing array using for_each
// using function
cout << "Multiple of 2 of elements are : ";
for_each(arr, arr + 5, printx2);
cout << endl;
// printing array using for_each
// using object function
cout << "Multiple of 3 of elements are : ";
for_each(arr, arr + 5, ob1);
cout << endl;
// initializing vector
vector<int> arr1 = { 4, 5, 8, 3, 1 };
cout << "Using Vectors:" << endl;
// printing array using for_each
// using function
cout << "Multiple of 2 of elements are : ";
for_each(arr1.begin(), arr1.end(), printx2);
cout << endl;
// printing array using for_each
// using object function
cout << "Multiple of 3 of elements are : ";
for_each(arr1.begin(), arr1.end(), ob1);
cout << endl;
}
Program 'takes the input' of a set of integers or characters. It firstly generates the random partition of the "Length of the Set". Starting from the beginning, it 'prints the number' of