Question: Consider the TooBig functor in Listing 16.15.What does the following code do, and what values get assigned to bo? bool bo = TooBig(10)(15); One functor

Consider the TooBig functor in Listing 16.15.What does the following code do, and what values get assigned to bo?  

bool bo = TooBig(10)(15);

Listing 16.15 functor.cpp // functor.cpp using a functor #include #include #include #include

One functor (f100) is a declared object, and the second (TooBig(200)) is an
anonymous object created by a constructor call. Here’s the output of the program in
Listing 16.15:
Original lists:
50 100 90 180 60 210 415 88 188 201
50 100 90 180 60 210 415 88 188 201
Trimmed lists:
50 100 90 60 88
50 100 90 180 60 88 188
Suppose that you already have a template function with two arguments:
template
bool tooBig(const T & val, const T & lim)
{
return val > lim;
}

You can use a class to convert it to a one-argument function object:
template
class TooBig2
{
private:
T cutoff;
public:
TooBig2(const T & t) : cutoff(t) {}
bool operator()(const T & v) { return tooBig(v, cutoff); }
};
That is, you can use the following:
TooBig2 tB100(100);
int x;
cin >> x;
if (tB100(x)) // same as if (tooBig(x,100))
...
So the call tB100(x) is the same as tooBig(x,100), but the two-argument function is
converted to a one-argument function object, with the second argument being used to
construct the function object. In short, the class functor TooBig2 is a function adapter that
adapts a function to meet a different interface.
As noted in the listing, C++11’s initializer-list feature simplifies initialization.You
can replace
int vals[10] = {50, 100, 90, 180, 60, 210, 415, 88, 188, 201};
list yadayada(vals, vals + 10); // range constructor
list etcetera(vals, vals + 10);
with this:
list yadayada = {50, 100, 90, 180, 60, 210, 415, 88, 188, 201};
list etcetera {50, 100, 90, 180, 60, 210, 415, 88, 188, 201};

Listing 16.15 functor.cpp // functor.cpp using a functor #include #include #include #include template // functor class defines operator () () class TooBig { private: T cutoff; public: TooBig (const T & t): cutoff (t) {} bool operator () (const T & v) { return v> cutoff; } }; void outint (int n) (std::cout < < I < < ";} int main() { } using std::list; using std::cout; using std::endl; " TooBig f100 (100); // limit = 100 int vals [10] = (50, 100, 90, 180, 60, 210, 415, 88, 188, 201}; list yadayada (vals, vals + 10); // range constructor list etcetera (vals, vals + 10); // C++11 can use the following instead // list yadayada = (50, 100, 90, 180, 60, 210, 415, 88, 188, 201); // list etcetera (50, 100, 90, 180, 60, 210, 415, 88, 188, 201}; cout < < "Original lists: "; for_each (yadayada.begin(), yadayada.end(), outint); cout < < endl; for_each (etcetera.begin(), etcetera.end(), outint); cout < < endl; yadayada.remove_if (f100); // use a named function object etcetera.remove_if (TooBig (200)); // construct a function object cout

Step by Step Solution

3.46 Rating (162 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

answer The code snippet bool bo TooBig 10 15 creates an instance of the f... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Introduction Java Program Questions!