One part of learning a language is getting to know the standard runtime / library environment. In all modern languages there are huge standard libraries and they make up a big part of what the language feels like, and how efficient you are as a developer in your day-to-day job.

In some languages these libraries are closely interwoven with the language itself. Take Java and it's String class. It's part of the library and located in the package java.lang. but it's so close to the language that every string literal is converted to a String instance.

Or take the Python and its Exception classes. Even when basic language expressions, like import package fail (e.g. because package cannot be found) an Exception from the package exceptions. is thrown.

C++ takes a somewhat different approach.

C++ is rigorously divided into core language features and a standard library that is implemented using these language features. C++ core language features are things like:

  • basic data types, arrays, pointers
  • functions
  • classes, inheritance, polymorphism
  • templates
  • exceptions
  • runtime type information

The standard library in C++ provides a number of mostly generic templates (classes and functions) and is called the Standard Template Library - STL.

The basic building blocks of the STL are containers, iterators and algorithms. Containers are standard computer science data structures - vectors, linked lists, priority queues, (hash) maps, sets, etc..

When using them, you will sometimes miss some helpfull member functions. E.g. there are no .sort, no .revert, no .search member function in the std::vector class. At first you'll find that really annoying. But like most of the things in C++, there is deeper meaning.

As said above, there is a collection of algorithms in the <algorithm> header. These standard algorithms like sort, replace, copy, for_each work on all these standard containers. Now why are they decoupled? The argument is simple and is called the NxM problem:

Given N containers and M algorithms, you have to maintain NxM implementations of these algorithms.

This is costly and error prone. The inventors of the STL felt the need to solve this problem and invented a glue, a unified interface that can expose some of the properties of containers so that generic algorithms can work on them. This glue is iterators.

An iterator is like a fancy reference/pointer to an element of a container. The weakest type of iterators supports the ++ increment operator and the * dereference operator. That means you can make the iterator point to the next element in the container and access what it's pointing to. The most powerful iterators expose a random access subscript operator []. Going on from this abstraction, you can implement algorithm template methods like std::copy that work equaly on raw C arrays and doubly linked lists. Yes, you can use the STL algorithms on raw C arrays.

Try this:

#include <algorithm>
#include <iostream>

int main(int, char**) {
  int foo[42];
  const size_t foo_size = sizeof(foo)/sizeof(foo[0]);
  int bar[foo_size];

  for(size_t i=0; i<foo_size; i++) {
    foo[i] = i;
  }

  std::copy(foo, foo+foo_size, bar);

  for(size_t i=0; i<foo_size; i++) {
    std::cout << bar[i] << " ";
  }

  return 0;
}

And next time, we'll take a look at the assembly :)