STANDARD TEMPLATE LIBRARY

The Standard Template Library (STL) is a cross-platform standard of efficient data structures and algorithms. It is one of the most important features of C++ and will form the basis of all ongoing quantitative finance programs within this book. The STL is often confusing to novice programmers because it makes use of the generic programming paradigm, as opposed to the object oriented paradigm. In fact, it could be said that the STL is the opposite approach, in that rather than encapsulate the data and algorithms acting upon it, the STL actually separates these functions through a common iterator interface.

The STL is extremely important in quantitative finance because many of the quantitative data structures and algorithms rely on more elementary concepts such as sorting, maxima/minima, copying, mathematical function modelling and searching. Rather than reinvent these basic algorithms from scratch, C++ provides them "out of the box". This allows you to spend more time testing your own algorithms, rather then "reinventing the wheel".

The STL is a large library. It contains a wealth of data structures and algorithms. In this article I will attempt to give a broad overview of the STL and highlight those aspects which are relevant to quantitative finance.

Components of the STL

The STL can be broadly split into four separate components:

There are extra smaller components, but these are the four that will interest us for quantitative finance work.

Containers

Containers allow certain types of data to be grouped into logical structures. The STL provides generic containers so that the same data structure can handle multiple different types without any additional required coding. For instance, we could create a std::vector<double> or a std::vector<int>. The former would be a vector of double precision values, while the latter is a vector of integer precision values. Notice that we are not required to code anything else to let the vector know we are using different types!

Sequence Containers

Sequence containers are extremely common within quantitative finance applications. They are often used to store numerical data sequentially. For instance we may want to store components of an element of a vector field or matrix of complex numbers. There are three main types of sequential containers that tend to be used the most:

There is also the valarray sequence container, which is (supposedly) optimised for numerical work. However, in practice valarray is harder to use and many modern compilers do not implement the optimisations well enough to warrant using them as an alternative to vectors.

Associative Containers

Associative containers are different to sequential containers in that there is no direct linear sequence to the elements within the container. Instead, they are sorted based on a comparison criterion. This criterion is often determined via the context. For instance, in a std::set<int>, the comparison operator is the less than (<) operator. There are four main types of associative container, which differ in terms of duplicate elements and whether the key data and value data coincide:

Although it might initially seem that associative containers are less appropriate for handling numerical data within quantitative finance, this is not actually the case. A map is ideal for holding the values of sparse matrices (such as tridiagonal or banded), which crop up in finite difference methods, for instance.

Container Adaptors

Although not strictly containers in themselves container adaptors are a very useful component of the STL. Container adaptors are designed to adapt the behaviour of the aforementioned containers above such that their behaviour is restricted or modified in some fashion. The three container adaptors that are of interest to us are as follows: