Share via


<algorithm>

Defines Standard Template Library (STL) container template functions that perform algorithms.

For a list of all members of this header, see <algorithm> Members.

namespace std {
template<class InputIterator, class Predicate>
    bool all_of (
        InputIterator _First, 
        InputIterator _Last,
        Predicate _Pred
    );
template<class InputIterator, class Predicate>
    bool any_of (
        InputIterator _First, 
        InputIterator _Last,
        Predicate _Pred
    );
template<class InputIterator, class Predicate>
    bool none_of (
        InputIterator _First, 
        InputIterator _Last,
        Predicate _Pred
    );
template<class InputIterator, class Function>
    Fn1 for_each (
        InputIterator _First, 
        InputIterator _Last, 
        Function _Func
    );
template<class InputIterator, class Type>
    InputIterator find (
        InputIterator _First, 
        InputIterator _Last, 
        const Type& _Val
    );
template<class InputIterator, class Predicate>
    InputIterator find_if (
        InputIterator _First, 
        InputIterator _Last, 
        Predicate _Pred
    );
template<class InputIterator, class Predicate>
    InputIterator find_if_not (
        InputIterator _First, 
        InputIterator _Last,
        Predicate _Pred
    ); 
template<class ForwardIterator1, class ForwardIterator2>
    ForwardIterator1 find_end (
        ForwardIterator1 _First1, 
        ForwardIterator1 _Last1,
        ForwardIterator2 _First2, 
        ForwardIterator2 _Last2
    );
template<class ForwardIterator1, class ForwardIterator2, 
         class Predicate>
    ForwardIterator1 find_end (
        ForwardIterator1 _First1, 
        ForwardIterator1 _Last1,
        ForwardIterator2 _First2, 
        ForwardIterator2 _Last2, 
        Predicate _Pred
    );
template<class InputIterator, class ForwardIterator>
    InputIterator1 find_first_of (
        InputIterator _First1, 
        InputIterator _Last1,
        ForwardIterator _First2, 
        ForwardIterator _Last2
    );
template<class InputIterator, class ForwardIterator, 
         class Predicate>
    InputIterator1 find_first_of (
        InputIterator _First1, 
        InputIterator _Last1,
        ForwardIterator _First2, 
        ForwardIterator _Last2, 
        Predicate _Pred
    );
template<class ForwardIterator>
    ForwardIterator adjacent_find (
        ForwardIterator _First, 
        ForwardIterator _Last
    );
template<class ForwardIterator, class Predicate>
    ForwardIterator adjacent_find (
        ForwardIterator _First, 
        ForwardIterator _Last, 
        Predicate _Pred
    );
template<class InputIterator, class Type>
    typename iterator_traits<InputIterator>::difference_type
        count (
            InputIterator _First, 
            InputIterator _Last,
            const Type& _Val
        );
template<class InputIterator, class Predicate>
    typename iterator_traits<InputIterator>::difference_type
        count_if (
            InputIterator _First, 
            InputIterator _Last,
            Predicate _Pred
        );
template<class InputIterator1, class InputIterator2>
    pair<InputIterator1, InputIterator2> 
        mismatch (
            InputIterator1 _First1, 
            InputIterator1 _Last1,
            InputIterator2 _First2
        );
template<class InputIterator1, class InputIterator2, class Predicate>
    pair<InputIterator1, InputIterator2> 
        mismatch (
            InputIterator1 _First1, 
            InputIterator1 _Last1,
            InputIterator2 _First2, 
            Predicate _Pred
        );
template<class InputIterator1, class InputIterator2>
    bool equal (
        InputIterator1 _First1, 
        InputIterator1 _Last1, 
        InputIterator2 _First2
    );
template<class InputIterator1, class InputIterator2, class Predicate>
    bool equal (
        InputIterator1 _First1, 
        InputIterator1 _Last1, 
        InputIterator2 _First2, 
        Predicate _Pred
    );
template<class ForwardIterator1, class ForwardIterator2>
    ForwardIterator1 search (
        ForwardIterator1 _First1, 
        ForwardIterator1 _Last1,
        ForwardIterator2 _First2, 
        ForwardIterator2 _Last2
    );
template<class ForwardIterator1, class ForwardIterator2, 
         class Predicate>
    ForwardIterator1 search (
        ForwardIterator1 _First1, 
        ForwardIterator1 _Last1,
        ForwardIterator2 _First2, 
        ForwardIterator2 _Last2, 
        Predicate _Pred
    );
template<class ForwardIterator, class Size, class Type>
    ForwardIterator search_n (
        ForwardIterator _First, 
        ForwardIterator _Last,
        Size _Count, 
        const Type& _Val
    );
template<class ForwardIterator, class Size, class Type, 
         class Predicate>
    ForwardIterator search_n (
        ForwardIterator _First, 
        ForwardIterator _Last,
        Size _Count, 
        const Type& _Val, 
        Predicate _Pred
    );

Remarks

The STL algorithms are generic because they can operate on a variety of data structures. The data structures that they can operate on include not only the STL container classes such as vector and list, but also program-defined data structures and arrays of elements that satisfy the requirements of a particular algorithm. STL algorithms achieve this level of generality by accessing and traversing the elements of a container indirectly through iterators.

STL algorithms process iterator ranges that are typically specified by their beginning or ending positions. The ranges referred to must be valid in the sense that all pointers in the ranges must be dereferenceable and, within the sequences of each range, the last position must be reachable from the first by incrementation.

The STL algorithms extend the actions supported by the operations and member functions of each STL container and allow working, for example, with different types of container objects at the same time. Two suffixes have been used to convey information about the purpose of the algorithms.

  • The if suffix indicates that the algorithm is used with function objects operating on the values of the elements rather than on the values of the elements themselves. The find_if algorithm looks for elements whose values satisfy the criterion specified by a function object, and the find algorithm searches for a particular value.

  • The _copy suffix indicates that the algorithm not only manipulates the values of the elements but also copies the modified values into a destination range. The reverse algorithm reverses the order of the elements within a range, and the reverse_copy algorithm also copies the result into a destination range.

STL algorithms are often classified into groups that indicate something about their purpose or requirements. These include modifying algorithms that change the value of elements as compared with nonmodifying algorithms that do not. Mutating algorithms change the order of elements, but not the values of their elements. Removing algorithms can eliminate elements from a range or a copy of a range. Sorting algorithms reorder the elements in a range in various ways and sorted range algorithms only act on algorithms whose elements have been sorted in a particular way.

The STL numeric algorithms that are provided for numerical processing have their own header file <numeric>, and function objects and adaptors are defined in the header <functional> Function objects that return Boolean values are known as predicates. The default binary predicate is the comparison operator<. In general, the elements being ordered need to be less than comparable so that, given any two elements, it can be determined either that they are equivalent (in the sense that neither is less than the other) or that one is less than the other. This results in an ordering among the nonequivalent elements.

See Also

Reference

Thread Safety in the Standard C++ Library

Standard Template Library

Other Resources

<algorithm> Members

Header Files