The pages on algorithms contain both a listing of all STL algorithms in alphabetical order, and all algorithms grouped by purpose. The alphabetical listing is the most detailed and complete and it is that listing that is described in what follows.

The entry for each algorithm shows each variation of the algorithm interface, and includes a brief description of what each version of the algorithm does.

Each algorithm entry also includes one or more links to sample programs illustrating the use of each algorithm version. So, for example, doit1a.cpp is the first sample program for the first (or only) listed interface of algorithm doit, while doit2c.cpp would be used for the name of the third sample program for the second interface of that same algorithm, if there were in fact such an interface and such a program.

Note that, for simplicity and consistency, unless there is a good reason to do otherwise, the first sample program illustrating any given algorithm will use one or more "vector of integer" containers only, wherever this is possible and makes sense. Additional sample programs for that algorithm, if any, may or may not use other kinds of containers, other kinds of component values, and/or other STL features.

To be most helpful in the shortest possible time, an algorithm is first shown as it would look in a typical invocation. However, each algorithm entry also includes the full prototype for that algorithm, or that version of the algorithm. The formatting style of algorithm prototypes is designed to provide maximum readability in the current context, not to be conformant to any particular C++ formatting style guidelines.

Note also that in the "typical invocation" form of an algorithm, the name of a parameter is meant to suggest its type. The purpose of each parameter is given in the brief description which follows the "typical invocation" form of the algorithm, according to the naming conventions used on this site. In the full prototype form of any algorithm, both parameter types and parameter names appear, so in this context a parameter name can be used to indicate the purpose of the parameter. Thus it may be helpful, even for a casual user, to look at both the typical invocation form and the full prototype form of any algorithm of interest.

Many algorithms are implemented as void functions, for which the return value is indicated as "none". For those that are not, a description of the value returned by the algorithm is given.

Most of the algorithms are made accessible by including the header file <algorithm> in your source code, but a few come from <numeric>. In actual fact, the algorithms in <numeric> are not technically part of the STL. However, they are fully compatible with the STL, and virtually indistinguishable from the "official" STL algorithms, and so are included here for the sake of completeness and convenience. Which of the two header files should be used to get access to any particular algorithm is indicated in the subsection header for that algorithm, following the name of the algorithm.

Once again, a reminder that before studying or using a particular algorithm it would be wise to familiarize yourself with the naming conventions used on this site.

Things to remember when using the algorithms

  1. Prefer a member function to a similarly named algorithm for performing a given task. If you have the choice of using either a member function or a similarly named algorithm to perform a given action on a container, it is generally better to use the member function, since the member function will be optimized for that container. For example, there is a lower_bound algorithm which can be used on any first-class container, but each of the associative containers has an analogous lower_bound member function which should be used in preference to the algorithm.
  2. Don't be afraid to use ordinary array pointers in a manner analogous to the use of iterators, where appropriate. Ordinary array pointers can often be used in the same context or situation where you might think an STL iterator would be required.
  3. You can generally use a more powerful iterator in place of a less powerful one, if it is more convenient or "natural" to do so. Remember, when actually using an algorithm, that a more powerful iterator can always be used in place of one that is less powerful. The STL iterators in order of decreasing "power" are: random access iterators, bidirectional iterators, forward iterators, input iterators, and output iterators. Thus, for example, if only a forward iterator is required in a particular algorithm, either a bidirectional iterator or a random access iterator may be used in its place.
  4. Ensure a container's size is large enough to accept all components being transferred into it. If you are using an algorithm (copy, for example) to copy or move components from one container to another, make sure that the destination container is large enough to accommodate those components, since in general the container will not grow as needed. Alternatively, you can make use of an inserter to grow the container as necessary, if one is available.