[   ^ to index...   |   next -->   ]

CSE 143/AC : 17 August 2000


The Standard Template Library

The standard template library (STL) contains three major interconnected parts:

  1. Containers: template classes that are responsible for flexibly managing collections of values of arbitrary type.

  2. Iterators: template classes that allow us to traverse and access elements in the container classes in a manner independent of both collection types and the element type.

  3. Algorithms: template functions and classes that provides common algorithms over containers, with minimal dependence on the element or container type.

It would be impossible to give even a mildly comprehensive overview of the STL in this class. Instead, we will walk through the usage of one container, with its associated iterator, and two algorithms.

std::vector

The template std::vector is a vector with operations that have the efficiency you would expect for a dynamic array implementation. That is, insertion at the beginning or middle is O(n), whereas insertion at the end and indexing are roughly constant time.

The actual definition of std::vector uses advanced C++ features that we haven't covered yet. Therefore, the following is a rough approximation of the class, "translated" into features you already know:

template <class T> vector { public: vector(); vector(const vector&); ~vector(); vector& operator=(const vector& x); // range-checked and non-range-checked element access T& at(int index); T& operator[](int index); // Stack-like operations. T& front(); T& back(); void push_back(T value); void pop_back(); // ... and lots more };

Last modified: Wed Aug 16 20:09:30 PDT 2000