Question

In: Computer Science

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);

Solutions

Expert Solution

Callback in C++

Callback or call-after function is any executable code that is passed as an argument to another

code,that other code is expected to execute(callback) the argument at a given time.

Example:

#include<iostream>

void A1() { //void is a return type like int

cout << "A1 function"; //print A1 function

}

void A2() {

cout <<"A2 function";

}

int main() {

void(*p) (); //function pointer

p= A1;

p() //output: A1 function

p=A2;

p(); //output: A2 function

}

3 kinds of Callback:

1) Function Pointer - See above example.

2) Function Objects/Functors

A functor is an object that acts like a function.

To create a functor ,we create a object that overloads the operator ().

Example code of Functor:

#include<iostream>

using namespace std;

class A1 {

int num; //variable declared

public:

A1() //constructor

{

num=0; //initialising num variable with 0

}

void print() {

cout<< num << endl; //output or print value of num variable on the output screen

}

void operator () (int val)

{

num+=val; //num=num+val

}

   };

int main() {

A1 a; //object declaration

a.print(); // 0

a(2); //Functor

a.print(); //2

a(8); //function object or Functor

a.print() //10

  return 0;

}

3) Lambda Expression

1- Anonymous notation representing something that performs calculation.

2- In Functional Programming ,it is used to produce first class and pure functions

3- This feature included in C++ 11.

3 Basic parts of Lambda Expression

1. Capturing List: [ ]

2. Parameter List: ( )

3. Body : { }

Order of above basic parts is - [ ] ( ) { }

1. bool notZero(int e){return e!=0;}

myLambda=[ ] (int e != 0) return true; //myLambda receives an integer and returns bool or boolean value(true or false)


Related Solutions

In C++ please. 5. Define container adapters that we studied. Explain why container adapters more suitable...
In C++ please. 5. Define container adapters that we studied. Explain why container adapters more suitable for some tasks than regular sequential container. State one container adapter and describe operations that are available for this adapter.
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.
Name the three (3) fundamental properties, or characteristics that we have studied for any production transformation...
Name the three (3) fundamental properties, or characteristics that we have studied for any production transformation process. (The blue-print of production). Then, provide a simple numerical example for each one of the three properties.
We have studied that assets have two kinds of risk: idiosyncratic (specific to the asset) and...
We have studied that assets have two kinds of risk: idiosyncratic (specific to the asset) and systemic (common to all assets). Why is the purpose of separating idiosyncratic risk from systemic risk? When you purchase an asset, do you consider the idiosyncratic risk from systemic risk? Given that systemic risk can be reduced/eliminated by diversification, how much value do you place on diversification?
2. (a) (b) Define viruses and virion (10 marks) (c) Name three main importance of fungi...
2. (a) (b) Define viruses and virion (c) Name three main importance of fungi (d) Explain briefly nucleocapsid Name 3 advantages and 3 disadvantages of normal flora Examiner: Prof. K. K. Addo e) Explain bacteriophages (f) What are lentiviruses
Please verify if correct. a. Company name—(7) the name that appears in the title bar of...
Please verify if correct. a. Company name—(7) the name that appears in the title bar of QuickBooks. The source is the My Company window. b. Ellipsis—(6) follows a command on a menu and means additional information must be entered on a second window called a dialog box. c. Activities-(4) the level of operation where most routine work is processed. d. QuickBooks—(1) a software package used to record business and accounting activities, designed with the non-accountant in mind. e. File name—(9)...
1. Define a. Asymmetric Information b. Adverse Selection c. Moral Hazard d. Name the three main...
1. Define a. Asymmetric Information b. Adverse Selection c. Moral Hazard d. Name the three main bond rating agencies
In the context of the two- period model without leisure we have studied in class, Define...
In the context of the two- period model without leisure we have studied in class, Define a competitive equilibrium as carefully as possible and indicate the elements involved in the definition. Make sure you clearly explain the problem and the the exogenous/endogenous variables for each decision maker.
Please show steps in the calculation. There are three kinds of apple sold in a supermarket,...
Please show steps in the calculation. There are three kinds of apple sold in a supermarket, namely Red Delicious, Fuji and Ambrosia. By experience, the chance of a customer purchases Fuji three times as much as each of the other two kinds. Suppose, there are 10 unrelated customers going to buy an apple and the supermarket management is interested in studying the sales of Fuji. Answer the following: (Hint: Binomial distribution) Show this is a Binomial experiment by stating the...
C&A produces three kinds of bread: wheat, white, and multigrain. The data on these products are...
C&A produces three kinds of bread: wheat, white, and multigrain. The data on these products are given below:   Wheat White Multigrain Demand (lbs per hour) 50 20 10 Changeover time (min) 60 30 45 Production rate (lbs per hour) 200 200 200 How many lbs of white bread should C&A produce with each batch in order to minimize inventory while satisfying demand? O 37.5 O 75 O 187.5 O 300
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT