1. Homepage of Dr. Zoltán Porkoláb
2. Teaching
3. Research
4. Affiliations

# 17. The Standard Template Library

Video: cpp-mat-ea17.mkv

The Standard Template Library (STL) is part of the C++ standard. The STL is the most important example for generic programming.

Generic programming: make more abstract routines without loosing efficiency using parameterization (both data and algorithm).

## Generics

Suppose we want to find an element in an array of integers:

For this purpose we can define a very specific implementation. We walk throu the interval and check every elements until we find the value we are looking for, or we find the end of the interval.

Consider the arguments of the find function: the begin pointer specifies the beginning of the interval, but end points the place after the last element. Thi sway the empty interval can be expressed more easily.

This function works, but too specific. It can be applied only for arrays of integers. We will generalize the function: we change it to accept more types, without compromising performance. Templates solve the problem:

This version of find works on array of doubles or even on strings. However, we are still restricted our algorithm to arrays.

Now we generalize on data structure. The idea is that on every data structure we can follow the same pseudo code:

1. Take the first element (begin)
2. Chack whether we reached the end of the interval ( begin != end )
3. If not, check whether this is the element we are looking for ( *begin == x )
4. If not, go to the next element of the interval ( ++begin )

What data structure dependent is how to reach the current element and how to go to the next element of the interval. To implement these concepts we introduce a new template type, the iterator, which is data structure dependent, and responsible to implement these operations.

Notice, that we return either an iterator referring to the element we found or end, the iterator value referring outside of the range. It is more general and still easy to check criteria to inform the user that the searched element was not found.

An iterator is the abstraction of the “pointer” concept, however, implemented usually as a (nested) class in a usually templated container. Now, we can use our find function in a data structure independent way:

Iterators are also have const version: const_iterator. The const_iterator behaves like a pointer to const, it is not a contant itself, but the referred memory is inmutable. Do not mix const iterator with const_iterator.

In C++11, we have a shortcut to declare iterators using automatic type deduction.

## Functors

When we want to execute more complex algorithms, we get some problems. Suppose, we want to find the third occurance of an element smaller than 55. This is not trivial with the original find algorithm.

There is an other find version: find_if which searches not for a concrete element, but the first element where a predicate is true:

To use find_if we first have to define a predicate, a function taking one parameter and return true when this parameter satisfy the required criteria.

The most simple (but far from perfect) implementation for the predicate is a simple function, which returns true when called the third time with an argument less than 55.

It seems working when we call first:

But in real situations this is a wrong implementation:

The problem is, that less55_3rd uses a local static variable cnt for counting, for functions this is the only feasible solution to remember the previous value of a local variable. Unfortunately, a static variable will keep its value between the separate calls of find_if too.

A further problem of static variables – locals or globals – is that they are also shared between parallel execution threads in a possible multithreaded application.

We need a solution in which we can allocate a separate memory for each applications of the function, separated from all – concurrent, or different in in time – applications of the same function. Classes do exactly this: data members provide separate memory space for each objects, while member functions provide the functionality.

The constructor initializes the cnt data member which conts the hits. The function call operator provides the actual checks – called on every data items on the range by the find_if algorithm. For each application of less55_3rd we should create a new object. Instead of declare a variable with name, it is enough to create a temporary variable.

Functors are flexible way to define arbitrary search, sort or other criteria by the user.

Remark: there is not always a good idea to create a functor with state. Some algorithms may copy or assign the functor parameter, therefore the state may changed differently as one suppose it.

One can generalize the less55_3rd functor to make it more generally usable. We can parameterize both the 55 and the 3 numeric parameters: instread of “wired-in” to the code they can be constructor parameters. Also we can generalize the types the functor works on.

Further generalization can be replace the operator< used in the function call operator with a functor parameter used to compare two elements with the less criteria.

Defining functors is a bit heavy weight solution since we have to write some boiler-plate code. The lambda functions can be used to replace functors with a much less syntactical overhead.

Since C++11, we can use lambda functions as functors.

Financed from the financial support ELTE won from the Higher Education Restructuring Fund of the Hungarian Government.