Online Book Reader

Home Category

Professional C__ - Marc Gregoire [227]

By Root 1415 0
to use lambda expressions, if possible, instead of function objects because lambdas are easier to use and easier to understand.

Arithmetic Function Objects

C++ provides functor class templates for the five binary arithmetic operators: plus, minus, multiplies, divides, and modulus. Additionally, unary negate is supplied. These classes are templatized on the type of the operands and are wrappers for the actual operators. They take one or two parameters of the template type, perform the operation, and return the result. Here is an example using the plus class template:

plus myPlus;

int res = myPlus(4, 5);

cout << res << endl;

Code snippet from FunctionObjects\Arithmetic.cpp

This example is silly, because there’s no reason to use the plus class template when you could just use operator+ directly. The benefit of the arithmetic function objects is that you can pass them as callbacks to algorithms, which you cannot do directly with the arithmetic operators. For example, the implementation of the geometricMean() function earlier in this chapter used the accumulate() function with a function pointer to the product() callback to multiply two integers. You could rewrite it to use the predefined multiplies function object:

double geometricMean(const vector& nums)

{

double mult = accumulate(nums.begin(), nums.end(), 1,

multiplies());

return pow(mult, 1.0 / nums.size());

}

Code snippet from FunctionObjects\Arithmetic.cpp

The expression multiplies() creates a new object of the multiplies functor class, instantiating it with the int type.

The other arithmetic function objects behave similarly.

The arithmetic function objects are just wrappers around the arithmetic operators. If you use the function objects as callbacks in algorithms, make sure that the objects in your container implement the appropriate operation, such as operator* or operator+.

Comparison Function Objects

In addition to the arithmetic function object classes, the C++ language provides all the standard comparisons: equal_to, not_equal_to, less, greater, less_equal, and greater_equal. You’ve already seen less in Chapter 12 as the default comparison for elements in the priority_queue and the associative containers. Now you can learn how to change that criterion. Here’s an example of a priority_queue using the default comparison operator: less.

priority_queue myQueue;

myQueue.push(3);

myQueue.push(4);

myQueue.push(2);

myQueue.push(1);

while (!myQueue.empty()) {

cout << myQueue.top() << " ";

myQueue.pop();

}

Code snippet from FunctionObjects\QueueLess.cpp

The output from the program looks like this:

4 3 2 1

As you can see, the elements of the queue are removed in descending order, according to the less comparison. You can change the comparison to greater by specifying it as the comparison template argument. The priority_queue template definition looks like this:

template , class Compare =

less >;

Unfortunately, the Compare type parameter is last, which means that in order to specify the comparison you must also specify the container. Here is an example of the above program modified so that the priority_queue sorts elements in ascending order using greater:

priority_queue, greater > myQueue;

myQueue.push(3);

myQueue.push(4);

myQueue.push(2);

myQueue.push(1);

while (!myQueue.empty()) {

cout << myQueue.top() << " ";

myQueue.pop();

}

Code snippet from FunctionObjects\QueueGreater.cpp

The output now is as follows:

1 2 3 4

Due to a syntactic restriction in pre-C++11 versions of C++, it was necessary to put a space between two angle brackets if they were not operator>>. This syntactic restriction has been removed in C++11, and spaces are no longer required. This is covered in the “Angle Brackets” section in Chapter 9. Therefore, the declaration of myQueue can be written without the formerly-required space, as follows:

priority_queue, greater> myQueue;

Several algorithms that you will learn about later in this chapter

Return Main Page Previous Page Next Page

®Online Book Reader