Tuesday, January 21, 2014

user defined generic function c++

STL user defined generic function c++ : Standard Template Library Tutorial, ccplusplus.com

User-Defined Generic Functions

The STL is an extensible framework. This means you can write your own functions and algorithms to process elements of collections. Of course, these operations may also be generic. However, to declare a valid iterator in these operations, you must use the type of the container, which is different for each container type. To facilitate the writing of generic functions, each container type provides some internal type definitions. Consider the following example:


This example defines a generic function that prints an optional string followed by all elements of the passed container. In the declaration


pos is declared as having the iterator type of the passed container type, typename is necessary to specify that const_iterator is a type and not a value of type T. In addition to iterator and const_iterator, containers provide other types to facilitate the writing of generic functions. For example, they provide the type of the elements to enable the handling of temporary copies of elements.

The optional second argument of PRINT_ELEMENTS is a string that is used as a prefix before all elements are written. Thus, by using PRINT_ELEMENTS() you could comment or introduce the output like this:



We introduced this function here because we will be using it often in the rest of the coming post to print all elements of containers by using a simple call.








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

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

No comments:

Post a Comment