In: Computer Science
In C++ please.
6. Define a callback. Name three types of callbacks used in STL algorithms. Given an container that contains integers. myCont Use count_if() algorithm and a lambda function as a callback to count the number of integers that are even. Explain the operation of your code.
Hi,
In c++ STL stands for Standard Template Library,where many operations and functions can take place.It contains a callback() function .A callback() function can be defined as a function that can be passed as an argument when any other function call the call back function.There are three types of callback used in STL they are:
A count_if() algorithm is an algorithm that returns the count that check the condition and satisfy the condition.A count_if() function consist of three parameters.They are lowerbound,upperbound and a function.It uses a boolean value in the function .
Here we need to count the number of integers that are even using count_if() and lamda functions.
#include<iostream>
int main()
{
n=count_if(begin(),end(),[](int n){return (n%2)==0;})
cout<<n;
}
Here it uses both count_if and lamda functions.lamda function is third parameter of count_If.It counts the number of even number . n is an even number if n%2 = 0;It counts the even number and store it in n.And n is printed.
Thank you..