Previous fileTop of DocumentContentsIndex pageNext file
Apache C++ Standard Library Reference Guide

Algorithms

Library:  Algorithms

Local Index

No Entries

Summary

Generic algorithms for performing various operations on containers and sequences

Synopsis

#include <algorithm>

namespace std {
  // synopsis of the entry
}

The synopsis of each algorithm appears in its entry in this Reference Guide.

Description

The C++ Standard Library allows you to apply generic algorithms to containers, and it supplies a set of these algorithms for searching, sorting, merging, transforming, scanning, and more.

Each algorithm can be applied to a variety of containers, including those defined by a user of the library. The following design features make algorithms generic:

In addition to requiring certain iterator capabilities, algorithms may require a container to be in a specific state. For example, some algorithms can only work on previously sorted containers.

Because most algorithms rely on iterators to gain access to data, they can be grouped according to the type of iterator they require, as is done in the Algorithms by Iterator section below. They can also be grouped according to the type of operation they perform.

Algorithms by Mutating/Non-mutating Function

The broadest categorization groups algorithms into two main types: mutating and non-mutating. Algorithms that mutate (alter) the contents of a container fall into the mutating group. All others are considered nonmutating. For example, both fill() and sort() are mutating algorithms, while find() and for_each() are nonmutating.

Nonmutating operations

Mutating operations

Note that the library has place and copy versions of many algorithms, such as replace() and replace_copy(). The library also has versions of algorithms that allow the use of default comparators and comparators supplied by the user. Often these functions are overloaded, but in cases where overloading proved impractical or impossible, the names differ. For example, replace() uses equality to determine replacement, and replace_if()accesses a user-provided compare function.

Algorithms by Operation

We can further distinguish algorithms by the kind of operations they perform. The following lists categorize algorithms by their operations.

Assigning operations

Search operations

Binary search operations (elements must be sorted)

Compare operations

Copy operations

Transforming operations

Swap operations

Scanning operations

Remove operations

Sorting operations

Merge operations (Elements must be sorted)

Set operations (Elements must be sorted)

Heap operations

Minimum and maximum

Permutation generators

next_permutation() prev_permutation()

Algorithms by Category

Each algorithm requires certain kinds of iterators. For a description of the iterators and their capabilities, see the Iterators entry in this manual. The following lists organize algorithms according to the types of iterators they require.

Algorithms that use no iterators:

Algorithms that require only input iterators:

Algorithms that require only output iterators:

Algorithms that read from input iterators and write to output iterators:

Algorithms that require forward iterators:

Algorithms that read from forward iterators and write to output iterators:

Algorithms that require bidirectional iterators

Algorithms that read from bidirectional iterators and write to output iterators:

Algorithms that require random access iterators:

Algorithms that read from input iterators and write to random access iterators:

Complexity

The complexity for each of these algorithms is given in the manual page for that algorithm.

See Also

Manual pages for each of the algorithms named in the lists above.

Standards Conformance

ISO/IEC 14882:1998 -- International Standard for Information Systems -- Programming Language C++, Section 25



Previous fileTop of DocumentContentsIndex pageNext file