Wednesday, January 11, 2012

unary predicates stl

unary predicates stl:

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:

Example:

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:
29 is first prime number found

See Also:

No comments:

Post a Comment