Tuesday, January 21, 2014

predicate function exa c++

STL predicate function C++ : Standard Template Library Tutorial, ccplusplus.com

Functions as Algorithm Arguments

To increase their flexibility and power, several algorithms allow the passing of user-defined auxiliary functions. These functions are called internally by the algorithms.

Examples of Using Functions as Algorithm Arguments

The simplest example is the for_each() algorithm. It calls a user-defined function for each element of the specified range. Consider the following example:


The for_each() algorithm calls the passed print() function for every element in the range [coll.begin(),coll.end()). Thus, the output of the program is as follows:


Algorithms use auxiliary functions in several variants—some optional, some mandatory. In particular, you can use them to specify a search criterion, a sorting criterion, or to define a manipulation while transferring elements from one collection to another.

Here is another example program:



In this example, square() is used to square each element of coll1 while it is transformed to coll2 (see figure below). The program has the following output:

Figure : How transform () operates




Predicates

A special kind of auxiliary function for algorithms is a predicate. Predicates are functions that return a Boolean value. They are often used to specify a sorting or a search criterion. Depending on their purpose, predicates are unary or binary. Note that not every unary or binary function that returns a Boolean value is a valid predicate. The STL requires that predicates always yield the same result for the same value. This rules out functions that modify their internal state when they are called.
Unary Predicates
Unary predicates check a specific property of a single argument. A typical example is a function that is used as a search criterion to find the first prime number:


In this example, the find_if() algorithm is used to search for the first element of the given range for which the passed unary predicate yields true. Here, the predicate is the isPrime() function. This function checks whether a number is a prime number. By using it, the algorithm returns the first prime number in the given range. If the algorithm does not find any element that matches the predicate, it returns the end of the range (its second argument). This is checked after the call. The collection in this example has a prime number between 24 and 30. So the output of the program is as follows:


Binary Predicates
Binary predicates typically compare a specific property of two arguments. For example, to sort elements according to your own criterion you could provide it as a simple predicate function. This might be necessary because the elements do not provide operator < or because you wish to use a different criterion.

The following example sorts elements of a set by the first name and last name of a person:



Note that you can also implement a sorting criterion as a function object. This kind of implementation has the advantage that the criterion is a type, which you could use, for example, to declare sets that use this criterion for sorting its elements.








-----------------------------------------------------------------
See Also:
-----------------------------------------------------------------

-----------------------------------------------------------------

No comments:

Post a Comment