Functions That Operate on Any Sequence
A major use of function templates is with parametrized classes like list. As you saw in the last section, specialized templates can be defined that will work with a container class such as list, for any type. The standard algorithms themselves are all function templates, so that they can be used with arguments of any type.
The idea of input and output sequences is very important in understanding and using the standard algorithms.
Sequences and for_each()
The standard algorithm for_each() is very simple. In the following implementation, it is given an input sequence and a function to apply to each element of that sequence:
template <class In, class Fn> void for_each(In i, In iend, Fn f) { for(; i != iend; ...
Get C++ By Example: UnderC Learning Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.