Question

In: Computer Science

In C++ please. 6. Define a callback. Name three types of callbacks used in STL algorithms....

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.

Solutions

Expert Solution

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:

  • Function pointer : In function pointer as a call back it assess then pointer when the function call occurs.
  • Function objects : A function objects is also called as functors.It is a n overloaded operator.It performs just like normal functions.
  • Lamda functions : It is another type of call back used in C++.It is like normal functions.It can pass argument and return the result like normal function but the difference is that this function doesn't have any name.

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..


Related Solutions

In C++ please. 7. Define a callback. Name three kinds of callbacks that we studied. Simplify...
In C++ please. 7. Define a callback. Name three kinds of callbacks that we studied. Simplify the following code using a lambda-expression. bool notZero(int e){return e!=0;} ... auto it=find_if(vect.begin(), vect.end(), notZero);
Please TYPE your answers: (a) Name the types of NONsampling error and define them in a...
Please TYPE your answers: (a) Name the types of NONsampling error and define them in a sentence or two. (b) Name the types of sampling error and define them in a sentence or two.
Please Use the STL library for this question and Should be done in C++ and Provide...
Please Use the STL library for this question and Should be done in C++ and Provide screenshots of the OUTPUT Topic: Stack Infix to postfix conversion         Take Input mathematical expression in infix notation, and convert it into a postfix notation. The infix notation may or may not have round parenthesis. Postfix expression evaluation         Take Input mathematical expression in postfix form, evaluate it, and display the result The mathematical expression can contain Numbers as operands (numbers can be of...
Define the three types of hypothesis tests and explain why they are used. Give an example...
Define the three types of hypothesis tests and explain why they are used. Give an example for each.
Define the three types of selection and provide an example of each. (please type for easier...
Define the three types of selection and provide an example of each. (please type for easier read)
C++ Analysis of Sorting Algorithms Design a class AbstractSort that can be used to analyze the...
C++ Analysis of Sorting Algorithms Design a class AbstractSort that can be used to analyze the number of comparisons performed by a sorting algorithm. The class should have a member function compare that is capable of comparing two array elements, and a means of keeping track of the number of comparisons performed. The class should be an abstract class with a pure virtual member function void sort(int arr[ ], int size) which, when overridden, will sort the array by calling...
Please answer in c++ 6.Define a function to find a given target value in an array,...
Please answer in c++ 6.Define a function to find a given target value in an array, but use pointer notation rather than array notation whenever possible. 7.Write a swap function, that swaps the values of two variables in main, but use pointers instead of reference parameters. 8.Write a function that takes an array of ints and its size as arguments. It should create a new array that is the same size as the argument. It should set the values in...
In C++ please. 6. Define iterator invalidation. Explain the reason for this invalidation. Point out the...
In C++ please. 6. Define iterator invalidation. Explain the reason for this invalidation. Point out the problem in the following code and correct it. vector <int> v = {10, 20, 30, 40, 50, 60}; int i; cout << "Enter number to remove from vector: "; cin >> i; for(auto it = v.begin(); it != v.end(); ++it) if(*it == i) v.erase(it);
Name and explain the 3 types of primary categories of Economic Indicators. Name and define two...
Name and explain the 3 types of primary categories of Economic Indicators. Name and define two specific examples for each indicator; and suggest what there movements during the past year might suggest re. the direction of the economy
What is “money”, exactly?   2. Please name three items that may be used as money, at...
What is “money”, exactly?   2. Please name three items that may be used as money, at least in theory, and name two items that could NEVER be used as money. What is the difference between these two groups?   3. What properties and characteristics should any type of money possess in order to be used as money?   4. What three functions should all types of money be able to perform?   5. What is a “barter” transaction, exactly? Please give an example...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT