Javatpoint Logo
Javatpoint Logo

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

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

Output:

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

Example 2

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.






Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


B.Tech / MCA