algorithm


adjacent_find · all_of · any_of · binary_search · copy · copy_backward · copy_if · copy_n · count · count_if · equal · equal_range · fill · fill_n · find · find_end · find_first_of · find_if · find_if_not · for_each · generate · generate_n · includes · inplace_merge · is_heap · is_heap_until · is_partitioned · is_permutation · is_sorted · is_sorted_until · iter_swap · lexicographical_compare · lower_bound · make_heap · max · max_element · merge · min · minmax · min_element · minmax_element · mismatch · move · move_backward · next_permutation · none_of · nth_element · partial_sort · partial_sort_copy · partition · partition_copy · partition_point · pop_heap · prev_permutation · push_heap · random_shuffle · remove · remove_copy · remove_copy_if · remove_if · replace · replace_copy · replace_copy_if · replace_if · reverse · reverse_copy · rotate · rotate_copy · search · search_n · set_difference · set_intersection · set_symmetric_difference · set_union · shuffle · sort · sort_heap · stable_partition · stable_sort · swap · swap_ranges · transform · unique · unique_copy · upper_bound


Include the STL standard header <algorithm> to define numerous template functions that perform useful algorithms. The descriptions that follow make extensive use of common template parameter names (or prefixes) to indicate the least powerful category of iterator permitted as an actual argument type:

The descriptions of these templates employ a number of conventions common to all algorithms.

namespace std {
template<class InIt, class Fn1>
    Fn1 for_each(InIt first, InIt last, Fn1 func);
template<class InIt, class Ty>
    InIt find(InIt first, InIt last, const Ty& val);
template<class InIt, class Pr>
    InIt find_if(InIt first, InIt last, Pr pred);
template<class FwdIt1, class FwdIt2>
    FwdIt1 find_end(FwdIt1 first1, FwdIt1 last1,
        FwdIt2 first2, FwdIt2 last2);
template<class FwdIt1, class FwdIt2, class Pr>
    FwdIt1 find_end(FwdIt1 first1, FwdIt1 last1,
        FwdIt2 first2, FwdIt2 last2, Pr pred);
template<class InIt1, class InIt2>
    InIt1 find_first_of(InIt1 first1, InIt1 last1,
        InIt2 first2, InIt2 last2);
template<class InIt1, class InIt2, class Pr>
    InIt1 find_first_of(InIt1 first1, InIt1 last1,
        InIt2 first2, InIt2 last2, Pr pred);
template<class FwdIt>
    FwdIt adjacent_find(FwdIt first, FwdIt last);
template<class FwdIt, class Pr>
    FwdIt adjacent_find(FwdIt first, FwdIt last, Pr pred);
template<class InIt, class Ty, class Dist>
    typename iterator_traits<InIt>::difference_type
        count(InIt first, InIt last,
            const Ty& val);
template<class InIt, class Pr, class Dist>
    typename iterator_traits<InIt>::difference_type
        count_if(InIt first, InIt last,
            Pr pred);

template<class InIt, class Pr>
    bool all_of(InIt first, InIt last,
        Pr pred); [added with C++11]
template<class InIt, class Pr>
    bool any_of(InIt first, InIt last,
        Pr pred); [added with C++11]
template<class InIt, class Pr>
    bool none_of(InIt first, InIt last,
        Pr pred); [added with C++11]
template<class InIt, class Pr>
    InIt find_if_not(InIt first, InIt last,
        Pr pred); [added with C++11]

template<class InIt1, class InIt2>
    pair<InIt1, InIt2> mismatch(InIt1 first1, InIt1 last1,
        InIt2 first2);
template<class InIt1, class InIt2, class Pr>
    pair<InIt1, InIt2> mismatch(InIt1 first1, InIt1 last1,
        InIt2 first2, Pr pred);
template<class InIt1, class InIt2>
    bool equal(InIt1 first1, InIt1 last1, InIt2 first2);
template<class InIt1, class InIt2, class Pr>
    bool equal(InIt1 first1, InIt1 last1, InIt2 first2, Pr pred);
template<class FwdIt1, class FwdIt2>
    FwdIt1 search(FwdIt1 first1, FwdIt1 last1,
        FwdIt2 first2, FwdIt2 last2);
template<class FwdIt1, class FwdIt2, class Pr>
    FwdIt1 search(FwdIt1 first1, FwdIt1 last1,
        FwdIt2 first2, FwdIt2 last2, Pr pred);
template<class FwdIt, class Diff, class Ty>
    FwdIt search_n(FwdIt first, FwdIt last,
        Diff count, const Ty& val);
template<class FwdIt, class Diff, class Ty, class Pr>
    FwdIt search_n(FwdIt first, FwdIt last,
        Diff count, const Ty& val, Pr pred);
template<class InIt, class OutIt>
    OutIt copy(InIt first, InIt last,
        OutIt dest);
template<class BidIt1, class BidIt2>
    BidIt2 copy_backward(BidIt1 first, BidIt1 last,
        BidIt2 dest);
template<class InIt, class OutIt, class Pr>
    OutIt copy_if(InIt first, InIt last,
        Pr pred); [added with C++11]
template<class InIt, class Diff, class OutIt>
    OutIt copy_n(InIt first, Diff count,
        OutIt dest); [added with C++11]

template<class InIt, class OutIt>
    OutIt move(InIt first, InIt last,
        OutIt dest); [added with C++11]
template<class BidIt1, class BidIt2>
    BidIt2 move_backward(BidIt1 first, BidIt1 last,
        BidIt2 dest); [added with C++11]
template<class Ty>
    void swap(Ty& left, Ty& right);
template<class Ty, size_t N>
    void swap(Ty (&left)[N], Ty (&right)[N]); [added with C++11]
template<class FwdIt1, class FwdIt2>
    FwdIt2 swap_ranges(FwdIt1 first1, FwdIt1 last1,
        FwdIt2 first2);
template<class FwdIt1, class FwdIt2>
    void iter_swap(FwdIt1 left, FwdIt2 right);
template<class InIt, class OutIt, class Fn1>
    OutIt transform(InIt first, InIt last, OutIt dest,
        Fn1 func);
template<class InIt1, class InIt2, class OutIt,
    class Fn2>
    OutIt transform(InIt1 first1, InIt1 last1,
        InIt2 first2, OutIt dest, Fn2 func);
template<class FwdIt, class Ty>
    void replace(FwdIt first, FwdIt last,
        const Ty& oldval, const Ty& newval);
template<class FwdIt, class Pr, class Ty>
    void replace_if(FwdIt first, FwdIt last,
        Pr pred, const Ty& val);
template<class InIt, class OutIt, class Ty>
    OutIt replace_copy(InIt first, InIt last, OutIt dest,
        const Ty& oldval, const Ty& newval);
template<class InIt, class OutIt, class Pr, class Ty>
    OutIt replace_copy_if(InIt first, InIt last, OutIt dest,
        Pr pred, const Ty& val);
template<class FwdIt, class Ty>
    void fill(FwdIt first, FwdIt last, const Ty& val);
template<class OutIt, class Diff, class Ty>
    OutIt fill_n(OutIt first, Diff count, const Ty& val);
template<class FwdIt, class Fn0>
    void generate(FwdIt first, FwdIt last, Fn0 func);
template<class OutIt, class Diff, class Fn0>
    OutIt generate_n(OutIt first, Diff count, Fn0 func);
template<class FwdIt, class Ty>
    FwdIt remove(FwdIt first, FwdIt last, const Ty& val);
template<class FwdIt, class Pr>
    FwdIt remove_if(FwdIt first, FwdIt last, Pr pred);
template<class InIt, class OutIt, class Ty>
    OutIt remove_copy(InIt first, InIt last, OutIt dest,
        const Ty& val);
template<class InIt, class OutIt, class Pr>
    OutIt remove_copy_if(InIt first, InIt last, OutIt dest,
        Pr pred);
template<class FwdIt>
    FwdIt unique(FwdIt first, FwdIt last);
template<class FwdIt, class Pr>
    FwdIt unique(FwdIt first, FwdIt last, Pr pred);
template<class InIt, class OutIt>
    OutIt unique_copy(InIt first, InIt last, OutIt dest);
template<class InIt, class OutIt, class Pr>
    OutIt unique_copy(InIt first, InIt last, OutIt dest,
        Pr pred);
template<class BidIt>
    void reverse(BidIt first, BidIt last);
template<class BidIt, class OutIt>
    OutIt reverse_copy(BidIt first, BidIt last, OutIt dest);
template<class FwdIt>
    FwdIt rotate(FwdIt first, FwdIt mid, FwdIt last);
template<class FwdIt, class OutIt>
    OutIt rotate_copy(FwdIt first, FwdIt mid,
        FwdIt last, OutIt dest);
template<class RanIt>
    void random_shuffle(RanIt first, RanIt last);
template<class RanIt, class Fn1>
    void random_shuffle(RanIt first, RanIt last,
        Fn1&& func);
template<class RanIt, class Urng>
    void shuffle(RanIt first, RanIt last,
        Urng&& func); [added with C++11]

template<class FwdIt, class Pr>
    FwdIt partition(FwdIt first, FwdIt last, Pr pred);
template<class InIt, class Pr>
    bool is_partitioned(InIt first, InIt last,
        Pr pred); [added with C++11]
template<class FwdIt, class Pr>
    FwdIt partition_point(FwdIt first, FwdIt last,
        Pr pred); [added with C++11]
template<class InIt, class OutIt1, class OutIt2, class Pr>
    pair<OutIt1, OutIt2>
        partition_copy(InIt first, InIt last,
            OutIt1 dest1, OutIt2 dest2, Pr pred); [added with C++11]

template<class BidIt, class Pr>
    BidIt stable_partition(BidIt first, BidIt last,
        Pr pred);
template<class RanIt>
    void sort(RanIt first, RanIt last);
template<class RanIt, class Pr>
    void sort(RanIt first, RanIt last, Pr pred);
template<class BidIt>
    void stable_sort(BidIt first, BidIt last);
template<class BidIt, class Pr>
    void stable_sort(BidIt first, BidIt last, Pr pred);
template<class RanIt>
    void partial_sort(RanIt first, RanIt mid,
        RanIt last);
template<class RanIt, class Pr>
    void partial_sort(RanIt first, RanIt mid,
        RanIt last, Pr pred);
template<class InIt, class RanIt>
    RanIt partial_sort_copy(InIt first1, InIt last1,
        RanIt first2, RanIt last2);
template<class InIt, class RanIt, class Pr>
    RanIt partial_sort_copy(InIt first1, InIt last1,
        RanIt first2, RanIt last2, Pr pred);
template<class RanIt>
    void nth_element(RanIt first, RanIt nth, RanIt last);
template<class RanIt, class Pr>
    void nth_element(RanIt first, RanIt nth, RanIt last,
        Pr pred);
template<class FwdIt, class Ty>
    FwdIt lower_bound(FwdIt first, FwdIt last,
        const Ty& val);
template<class FwdIt, class Ty, class Pr>
    FwdIt lower_bound(FwdIt first, FwdIt last,
        const Ty& val, Pr pred);
template<class FwdIt, class Ty>
    FwdIt upper_bound(FwdIt first, FwdIt last,
        const Ty& val);
template<class FwdIt, class Ty, class Pr>
    FwdIt upper_bound(FwdIt first, FwdIt last,
        const Ty& val, Pr pred);
template<class FwdIt, class Ty>
    pair<FwdIt, FwdIt> equal_range(FwdIt first,
        FwdIt last, const Ty& val);
template<class FwdIt, class Ty, class Pr>
    pair<FwdIt, FwdIt> equal_range(FwdIt first,
        FwdIt last, const Ty& val, Pr pred);
template<class FwdIt, class Ty>
    bool binary_search(FwdIt first, FwdIt last,
        const Ty& val);
template<class FwdIt, class Ty, class Pr>
    bool binary_search(FwdIt first, FwdIt last,
        const Ty& val, Pr pred);
template<class InIt1, class InIt2, class OutIt>
    OutIt merge(InIt1 first1, InIt1 last1,
        InIt2 first2, InIt2 last2, OutIt dest);
template<class InIt1, class InIt2, class OutIt,
    class Pr>
    OutIt merge(InIt1 first1, InIt1 last1,
        InIt2 first2, InIt2 last2, OutIt dest, Pr pred);
template<class BidIt>
    void inplace_merge(BidIt first, BidIt mid,
        BidIt last);
template<class BidIt, class Pr>
    void inplace_merge(BidIt first, BidIt mid,
        BidIt last, Pr pred);
template<class InIt1, class InIt2>
    bool includes(InIt1 first1, InIt1 last1,
        InIt2 first2, InIt2 last2);
template<class InIt1, class InIt2, class Pr>
    bool includes(InIt1 first1, InIt1 last1,
        InIt2 first2, InIt2 last2, Pr pred);
template<class InIt1, class InIt2, class OutIt>
    OutIt set_union(InIt1 first1, InIt1 last1,
        InIt2 first2, InIt2 last2, OutIt dest);
template<class InIt1, class InIt2, class OutIt,
    class Pr>
    OutIt set_union(InIt1 first1, InIt1 last1,
        InIt2 first2, InIt2 last2, OutIt dest, Pr pred);
template<class InIt1, class InIt2, class OutIt>
    OutIt set_intersection(InIt1 first1, InIt1 last1,
        InIt2 first2, InIt2 last2, OutIt dest);
template<class InIt1, class InIt2, class OutIt,
    class Pr>
    OutIt set_intersection(InIt1 first1, InIt1 last1,
        InIt2 first2, InIt2 last2, OutIt dest, Pr pred);
template<class InIt1, class InIt2, class OutIt>
    OutIt set_difference(InIt1 first1, InIt1 last1,
        InIt2 first2, InIt2 last2, OutIt dest);
template<class InIt1, class InIt2, class OutIt,
    class Pr>
    OutIt set_difference(InIt1 first1, InIt1 last1,
        InIt2 first2, InIt2 last2, OutIt dest, Pr pred);
template<class InIt1, class InIt2, class OutIt>
    OutIt set_symmetric_difference(InIt1 first1,
        InIt1 last1, InIt2 first2, InIt2 last2, OutIt dest);
template<class InIt1, class InIt2, class OutIt,
    class Pr>
    OutIt set_symmetric_difference(InIt1 first1,
        InIt1 last1, InIt2 first2, InIt2 last2, OutIt dest,
            Pr pred);
template<class RanIt>
    void push_heap(RanIt first, RanIt last);
template<class RanIt, class Pr>
    void push_heap(RanIt first, RanIt last, Pr pred);
template<class RanIt>
    void pop_heap(RanIt first, RanIt last);
template<class RanIt, class Pr>
    void pop_heap(RanIt first, RanIt last, Pr pred);
template<class RanIt>
    void make_heap(RanIt first, RanIt last);
template<class RanIt, class Pr>
    void make_heap(RanIt first, RanIt last, Pr pred);
template<class RanIt>
    void sort_heap(RanIt first, RanIt last);
template<class RanIt, class Pr>
    void sort_heap(RanIt first, RanIt last, Pr pred);

template<class Ty>
    const Ty& max(const Ty& left, const Ty& right);
template<class Ty>
    const Ty& max(initializer_list<Ty> init); [added with C++11]
template<class Ty, class Pr>
    const Ty& max(const Ty& left, const Ty& right, Pr pred);
template<class Ty, class Pr>
    const Ty& max(initializer_list<Ty> init, Pr pred); [added with C++11]

template<class Ty>
    const Ty& min(const Ty& left, const Ty& right);
template<class Ty>
    const Ty& min(initializer_list<Ty> init); [added with C++11]
template<class Ty, class Pr>
    const Ty& min(const Ty& left, const Ty& right, Pr pred);
template<class Ty, class Pr>
    const Ty& min(initializer_list<Ty> init, Pr pred); [added with C++11]

template<class Ty>
    pair<const Ty&, const Ty&>
        minmax(const Ty& left, const Ty& right); [added with C++11]
template<class Ty>
    pair<const Ty&, const Ty&>
        minmax(initializer_list<Ty> init); [added with C++11]
template<class Ty, class Pr>
    pair<const Ty&, const Ty&>
        minmax(const Ty& left, const Ty& right, Pr pred); [added with C++11]
template<class Ty, class Pr>
    pair<const Ty&, const Ty&>
        minmax(initializer_list<Ty> init, Pr pred); [added with C++11]

template<class FwdIt>
    FwdIt max_element(FwdIt first, FwdIt last);
template<class FwdIt, class Pr>
    FwdIt max_element(FwdIt first, FwdIt last, Pr pred);
template<class FwdIt>
    FwdIt min_element(FwdIt first, FwdIt last);
template<class FwdIt, class Pr>
    FwdIt min_element(FwdIt first, FwdIt last, Pr pred);
template<class FwdIt>
    pair<FwdIt, FwdIt>
        minmax_element(FwdIt first, FwdIt last); [added with C++11]
template<class FwdIt, class Pr>
    pair<FwdIt, FwdIt>
        minmax_element(FwdIt first, FwdIt last, Pr pred); [added with C++11]

template<class InIt1, class InIt2>
    bool lexicographical_compare(InIt1 first1,
        InIt1 last1, InIt2 first2, InIt2 last2);
template<class InIt1, class InIt2, class Pr>
    bool lexicographical_compare(InIt1 first1,
        InIt1 last1, InIt2 first2, InIt2 last2, Pr pred);
template<class BidIt>
    bool next_permutation(BidIt first, BidIt last);
template<class BidIt, class Pr>
    bool next_permutation(BidIt first, BidIt last,
        Pr pred);
template<class BidIt>
    bool prev_permutation(BidIt first, BidIt last);
template<class BidIt, class Pr>
    bool prev_permutation(BidIt first, BidIt last,
        Pr pred);

template<class RanIt>
    RanIt is_heap_until(RanIt first, RanIt last); [added with C++11]
template<class RanIt, class Pr>
    RanIt is_heap_until(RanIt first, RanIt last, Pr pred); [added with C++11]
template<class RanIt>
    bool is_heap(RanIt first, RanIt last); [added with C++11]
template<class RanIt, class Pr>
    bool is_heap(RanIt first, RanIt last, Pr pred); [added with C++11]

template<class FwdIt>
    FwdIt is_sorted_until(RanIt first, RanIt last); [added with C++11]
template<class FwdIt, class Pr>
    FwdIt is_sorted_until(RanIt first, RanIt last, Pr pred); [added with C++11]
template<class FwdIt>
    bool is_sorted(RanIt first, RanIt last); [added with C++11]
template<class FwdIt, class Pr>
    bool is_sorted(RanIt first, RanIt last, Pr pred); [added with C++11]

template<class FwdIt1, class FwdIt2>
    bool is_permutation(FwdIt first1, FwdIt last1,
        FwdIt first2); [added with C++11]
template<class FwdIt1, class FwdIt2, class Pr>
    bool is_permutation(FwdIt first1, FwdIt last1,
        FwdIt first2, Pr pred); [added with C++11]
}  // namespace std

adjacent_find

template<class FwdIt>
    FwdIt adjacent_find(FwdIt first, FwdIt last);
template<class FwdIt, class Pr>
    FwdIt adjacent_find(FwdIt first, FwdIt last, Pr pred);

The first template function determines the lowest N in the range [0, last - first) for which N + 1 < last - first and the predicate *(first + N) == *(first + N + 1) is true. Here, operator== must perform a pairwise comparison between its operands. It then returns first + N. If no such value exists, the function returns last. If the sequence contains fewer than two elements, the function never evaluates the predicate. Otherwise, if it returns last, it evaluates the predicate exactly last - first - 1 times. Otherwise, it evaluates the predicate exactly N + 1 times.

The second template function behaves the same, except that the predicate is pred(*(first + N), *(first + N + 1)).

all_of

template<class InIt, class Pr>
    bool all_of(InIt first, InIt last,
        Pr pred); [added with C++11]

The template function returns true only if, for each N in the range [0, last - first), the predicate pred(*(first + N)) is true.

any_of

template<class InIt, class Pr>
    bool any_of(InIt first, InIt last,
        Pr pred); [added with C++11]

The template function returns true only if, for some N in the range [0, last - first), the predicate pred(*(first + N)) is true.

binary_search

template<class FwdIt, class Ty>
    bool binary_search(FwdIt first, FwdIt last,
        const Ty& val);
template<class FwdIt, class Ty, class Pr>
    bool binary_search(FwdIt first, FwdIt last,
        const Ty& val, Pr pred);

The first template function determines whether a value of N exists in the range [0, last - first) for which *(first + N) has equivalent ordering to val, where the elements designated by iterators in the range [first, last) form a sequence partitioned by operator<. If so, the function returns true. If no such value exists, it returns false.

The function evaluates the ordering predicate X < Y at most ceil(log(last - first)) + O(1) times.

The second template function behaves the same, except that it replaces operator<(X, Y) with pred(X, Y).

copy

template<class InIt, class OutIt>
    OutIt copy(InIt first, InIt last,
        OutIt dest);

The template function evaluates *(dest + N) = *(first + N)) once for each N in the range [0, last - first), for strictly increasing values of N beginning with the lowest value. It then returns dest + N. If dest and first designate regions of storage, dest must not be in the range [first, last).

copy_backward

template<class BidIt1, class BidIt2>
    BidIt2 copy_backward(BidIt1 first, BidIt1 last,
        BidIt2 dest);

The template function evaluates *(dest - N - 1) = *(last - N - 1)) once for each N in the range [0, last - first), for strictly increasing values of N beginning with the lowest value. It then returns dest - (last - first). If dest and first designate regions of storage, dest must not be in the range (first, last).

copy_if

template<class InIt, class OutIt, class Pr>
    OutIt copy_if(InIt first, InIt last,
        Pr pred); [added with C++11]

The template function evaluates:

if (pred(*dest))
    *dest++ = *(first + N))

once for each N in the range [0, last - first), for strictly increasing values of N beginning with the lowest value. It then returns dest. If dest and first designate regions of storage, dest must not be in the range [first, last).

copy_n

template<class InIt, class Diff, class OutIt>
    OutIt copy_n(InIt first, Diff count,
        OutIt dest); [added with C++11]

The template function evaluates *(dest + N) = *(first + N)) once for each N in the range [0, count), for strictly increasing values of N beginning with the lowest value. It then returns dest + N. If dest and first designate regions of storage, dest must not be in the range [first, last).

count

template<class InIt, class Ty>
    typename iterator_traits<InIt>::difference_type
        count(InIt first, InIt last, const Ty& val);

The template function sets a count count to zero. It then executes ++count for each N in the range [0, last - first) for which the predicate *(first + N) == val is true. Here, operator== must perform a pairwise comparison between its operands. The function returns count. It evaluates the predicate exactly last - first times.

count_if

template<class InIt, class Pr, class Dist>
    typename iterator_traits<InIt>::difference_type
        count_if(InIt first, InIt last,
            Pr pred);

The template function sets a count count to zero. It then executes ++count for each N in the range [0, last - first) for which the predicate pred(*(first + N)) is true. The function returns count. It evaluates the predicate exactly last - first times.

equal

template<class InIt1, class InIt2>
    bool equal(InIt1 first1, InIt1 last1, InIt2 first2);
template<class InIt1, class InIt2, class Pr>
    bool equal(InIt1 first1, InIt1 last1, InIt2 first2, Pr pred);

The first template function returns true only if, for each N in the range [0, last1 - first1), the predicate *(first1 + N) == *(first2 + N) is true. Here, operator== must perform a pairwise comparison between its operands. The function evaluates the predicate at most once for each N.

The second template function behaves the same, except that the predicate is pred(*(first1 + N), *(first2 + N)).

equal_range

template<class FwdIt, class Ty>
    pair<FwdIt, FwdIt> equal_range(FwdIt first,
        FwdIt last, const Ty& val);
template<class FwdIt, class Ty, class Pr>
    pair<FwdIt, FwdIt> equal_range(FwdIt first,
        FwdIt last, const Ty& val, Pr pred);

The first template function effectively returns pair( lower_bound(first, last, val), upper_bound(first, last, val)), where the elements designated by iterators in the range [first, last) form a sequence partitioned by operator<. Thus, the function determines the largest range of positions over which val can be inserted in the sequence and still preserve its ordering.

The function evaluates the ordering predicate X < Y at most ceil(2 * log(last - first)) + 2.

The second template function behaves the same, except that it replaces operator<(X, Y) with pred(X, Y).

fill

template<class FwdIt, class Ty>
    void fill(FwdIt first, FwdIt last, const Ty& val);

The template function evaluates *(first + N) = val once for each N in the range [0, last - first).

fill_n

template<class OutIt, class Diff, class Ty>
    OutIt fill_n(OutIt first, Diff count, const Ty& val);

The template function evaluates *(first + N) = val once for each N in the range [0, count), then returns first incremented count times.

find

template<class InIt, class Ty>
    InIt find(InIt first, InIt last, const Ty& val);

The template function determines the lowest value of N in the range [0, last - first) for which the predicate *(first + N) == val is true. Here, operator== must perform a pairwise comparison between its operands. It then returns first + N. If no such value exists, the function returns last. It evaluates the predicate at most once for each N.

find_end

template<class FwdIt1, class FwdIt2>
    FwdIt1 find_end(FwdIt1 first1, FwdIt1 last1,
        FwdIt2 first2, FwdIt2 last2);
template<class FwdIt1, class FwdIt2, class Pr>
    FwdIt1 find_end(FwdIt1 first1, FwdIt1 last1,
        FwdIt2 first2, FwdIt2 last2, Pr pred);

The first template function determines the highest value of N in the range [0, last1 - first1 - (last2 - first2)) such that for each M in the range [0, last2 - first2), the predicate *(first1 + N + M) == *(first2 + N + M) is true. Here, operator== must perform a pairwise comparison between its operands. It then returns first1 + N. If no such value exists, the function returns last1. It evaluates the predicate at most (last2 - first2) * (last1 - first1 - (last2 - first2) + 1) times.

The second template function behaves the same, except that the predicate is pred(*(first1 + N + M), *(first2 + N + M)).

find_first_of

template<class InIt1, class InIt2>
    InIt1 find_first_of(InIt1 first1, InIt1 last1,
        InIt2 first2, InIt2 last2);
template<class InIt1, class InIt2, class Pr>
    InIt1 find_first_of(InIt1 first1, InIt1 last1,
        InIt2 first2, InIt2 last2, Pr pred);

The first template function determines the lowest value of N in the range [0, last1 - first1) such that for some M in the range [0, last2 - first2), the predicate *(first1 + N) == *(first2 + M) is true. Here, operator== must perform a pairwise comparison between its operands. It then returns first1 + N. If no such value exists, the function returns last1. It evaluates the predicate at most (last1 - first1) * (last2 - first2) times.

The second template function behaves the same, except that the predicate is pred(*(first1 + N), *(first2 + M)).

find_if

template<class InIt, class Pr>
    InIt find_if(InIt first, InIt last, Pr pred);

The template function determines the lowest value of N in the range [0, last - first) for which the predicate pred(*(first + N)) is true. It then returns first + N. If no such value exists, the function returns last. It evaluates the predicate at most once for each N.

find_if_not

template<class InIt, class Pr>
    InIt find_if_not(InIt first, InIt last,
        Pr pred); [added with C++11]

The template function determines the lowest value of N in the range [0, last - first) for which the predicate pred(*(first + N)) is false. It then returns first + N. If no such value exists, the function returns last. It evaluates the predicate at most once for each N.

for_each

template<class InIt, class Fn1>
    Fn1 for_each(InIt first, InIt last, Fn1 func);

The template function evaluates func(*(first + N)) once for each N in the range [0, last - first). It then returns move(func).

generate

template<class FwdIt, class Fn0>
    void generate(FwdIt first, FwdIt last, Fn0 func);

The template function evaluates *(first + N) = func() once for each N in the range [0, last - first).

generate_n

template<class OutIt, class Diff, class Fn0>
    OutIt generate_n(OutIt first, Diff count, Fn0 func);

The template function evaluates *(first + N) = func() once for each N in the range [0, count), then returns first incremented count times.

includes

template<class InIt1, class InIt2>
    bool includes(InIt1 first1, InIt1 last1,
        InIt2 first2, InIt2 last2);
template<class InIt1, class InIt2, class Pr>
    bool includes(InIt1 first1, InIt1 last1,
        InIt2 first2, InIt2 last2, Pr pred);

The first template function determines whether a value of N exists in the range [0, last2 - first2) such that, for each M in the range [0, last1 - first1), *(first1 + M) and *(first2 + N) do not have equivalent ordering, where the elements designated by iterators in the ranges [first1, last1) and [first2, last2) each form a sequence ordered by operator<. If so, the function returns false. If no such value exists, it returns true. Thus, the function determines whether the ordered sequence designated by iterators in the range [first2, last2) all have equivalent ordering with some element designated by iterators in the range [first1, last1).

The function evaluates the predicate at most 2 * ((last1 - first1) + (last2 - first2)) - 1 times.

The second template function behaves the same, except that it replaces operator<(X, Y) with pred(X, Y).

inplace_merge

template<class BidIt>
    void inplace_merge(BidIt first, BidIt mid,
        BidIt last);
template<class BidIt, class Pr>
    void inplace_merge(BidIt first, BidIt mid,
        BidIt last, Pr pred);

The first template function reorders the sequences designated by iterators in the ranges [first, mid) and [mid, last), each ordered by operator<, to form a merged sequence of length last - first beginning at first also ordered by operator<. The merge occurs without altering the relative order of elements within either original sequence. Moreover, for any two elements from different original sequences that have equivalent ordering, the element from the ordered range [first, mid) precedes the other.

The function evaluates the ordering predicate X < Y at most ceil((last - first) * log(last - first)) times. (Given enough temporary storage, it can evaluate the predicate at most (last - first) - 1 times.)

The second template function behaves the same, except that it replaces operator<(X, Y) with pred(X, Y).

Beginning with C++11, the affected objects need only be movable, not copyable.

is_heap

template<class RanIt>
    bool is_heap(RanIt first, RanIt last); [added with C++11]
template<class RanIt, class Pr>
    bool is_heap(RanIt first, RanIt last, Pr pred); [added with C++11]

The first template function returns is_heap_until(first, last) == last.

The second template function returns is_heap_until(first, last, pred) == last.

is_heap_until

template<class RanIt>
    bool is_heap_until(RanIt first, RanIt last); [added with C++11]
template<class RanIt, class Pr>
    bool is_heap_until(RanIt first, RanIt last, Pr pred); [added with C++11]

The first template function returns the last iterator next in [first, last) such that [first, next) is a heap ordered by operator< or by pred. If last - next < 2 the function returns last.

The second template function behaves the same, except that it replaces operator<(X, Y) with pred(X, Y).

is_partitioned

template<class InIt, class Pr>
    bool is_partitioned(InIt first, InIt last,
        Pr pred); [added with C++11]

The template function returns true only if all elements in [first, last) are partitioned by pred; that is, all elements X in [first, last) for which pred(X) is true occur before all elements Y for which pred(Y) is false.

is_permutation

template<class FwdIt1, class FwdIt2>
    bool is_permutation(FwdIt first1, FwdIt last1,
        FwdIt first2); [added with C++11]
template<class FwdIt1, class FwdIt2, class Pr>
    bool is_permutation(FwdIt first1, FwdIt last1,
        FwdIt first2, Pr pred); [added with C++11]

The first template function assumes that there are as many elements in the range beginning at first2 as there are in the range designated by [first1, last1). It returns true only if, for each element X in the range designated by [first1, last1) there are as many elements Y in the same range for which X == Y as there are in the range beginning at first2. Here, operator== must perform a pairwise comparison between its operands.

The second template function behaves the same, except that it replaces operator==(X, Y) with pred(X, Y).

is_sorted

template<class FwdIt>
    bool is_sorted(FwdIt first, FwdIt last); [added with C++11]
template<class FwdIt, class Pr>
    bool is_sorted(FwdIt first, FwdIt last, Pr pred); [added with C++11]

The first template function returns is_sorted_until(first, last) == last.

The second template function returns is_sorted_until(first, last, pred) == last.

is_sorted_until

template<class FwdIt>
    bool is_sorted_until(FwdIt first, FwdIt last); [added with C++11]
template<class FwdIt, class Pr>
    bool is_sorted_until(FwdIt first, FwdIt last, Pr pred); [added with C++11]

The first template function returns the last iterator next in [first, last) such that [first, next) is a sorted ordered by operator< or pred. If last - next < 2 the function returns last.

The second template function behaves the same, except that it replaces operator<(X, Y) with pred(X, Y).

iter_swap

template<class FwdIt1, class FwdIt2>
    void iter_swap(FwdIt1 left, FwdIt2 right);

The template function leaves the value originally stored in *right subsequently stored in *left, and the value originally stored in *left subsequently stored in *right.

lexicographical_compare

template<class InIt1, class InIt2>
    bool lexicographical_compare(InIt1 first1,
        InIt1 last1, InIt2 first2, InIt2 last2);
template<class InIt1, class InIt2, class Pr>
    bool lexicographical_compare(InIt1 first1,
        InIt1 last1, InIt2 first2, InIt2 last2, Pr pred);

The first template function determines K, the number of elements to compare as the smaller of last1 - first1 and last2 - first2. It then determines the lowest value of N in the range [0, K) for which *(first1 + N) and *(first2 + N) do not have equivalent ordering. If no such value exists, the function returns true only if K < (last2 - first2). Otherwise, it returns true only if *(first1 + N) < *(first2 + N). Thus, the function returns true only if the sequence designated by iterators in the range [first1, last1) is lexicographically less than the other sequence.

The function evaluates the ordering predicate X < Y at most 2 * K times.

The second template function behaves the same, except that it replaces operator<(X, Y) with pred(X, Y).

lower_bound

template<class FwdIt, class Ty>
    FwdIt lower_bound(FwdIt first, FwdIt last,
        const Ty& val);
template<class FwdIt, class Ty, class Pr>
    FwdIt lower_bound(FwdIt first, FwdIt last,
        const Ty& val, Pr pred);

The first template function determines the highest value of N in the range (0, last - first] such that, for each M in the range [0, N) the predicate *(first + M) < val is true, where the elements designated by iterators in the range [first, last) form a sequence partitioned by operator<. It then returns first + N. Thus, the function determines the lowest position before which val can be inserted in the sequence and still preserve its ordering.

The function evaluates the ordering predicate X < Y at most ceil(log(last - first)) + 1 times.

The second template function behaves the same, except that it replaces operator<(X, Y) with pred(X, Y).

make_heap

template<class RanIt>
    void make_heap(RanIt first, RanIt last);
template<class RanIt, class Pr>
    void make_heap(RanIt first, RanIt last, Pr pred);

The first template function reorders the sequence designated by iterators in the range [first, last) to form a heap ordered by operator<.

The function evaluates the ordering predicate X < Y at most 3 * (last - first) times.

The second template function behaves the same, except that it replaces operator<(X, Y) with pred(X, Y).

Beginning with C++11, the affected objects need only be movable, not copyable.

max

template<class Ty>
    const Ty& max(const Ty& left, const Ty& right);
template<class Ty>
    Ty max(initializer_list<Ty> init); [added with C++11]
template<class Ty, class Pr>
    const Ty& max(const Ty& left, const Ty& right, Pr pred);
template<class Ty, class Pr>
    Ty max(initializer_list<Ty> init, Pr pred); [added with C++11]

The first template function returns right if left < right. Otherwise it returns left. Ty need supply only a single-argument constructor and a destructor.

The second template function returns the leftmost element of init that compares no smaller than any other element.

The remaining template functions behaves the same, except that they replace operator<(X, Y) with pred(X, Y).

max_element

template<class FwdIt>
    FwdIt max_element(FwdIt first, FwdIt last);
template<class FwdIt, class Pr>
    FwdIt max_element(FwdIt first, FwdIt last, Pr pred);

The first template function determines the lowest value of N in the range [0, last - first) such that, for each M in the range [0, last - first) the predicate *(first + N) < *(first + M) is false. It then returns first + N. Thus, the function determines the lowest position that contains the largest value in the sequence.

The function evaluates the ordering predicate X < Y exactly max((last - first) - 1, 0) times.

The second template function behaves the same, except that it replaces operator<(X, Y) with pred(X, Y).

merge

template<class InIt1, class InIt2, class OutIt>
    OutIt merge(InIt1 first1, InIt1 last1,
        InIt2 first2, InIt2 last2, OutIt dest);
template<class InIt1, class InIt2, class OutIt,
    class Pr>
    OutIt merge(InIt1 first1, InIt1 last1,
        InIt2 first2, InIt2 last2, OutIt dest, Pr pred);

The first template function determines K, the number of elements to copy as (last1 - first1) + (last2 - first2). It then alternately copies two sequences, designated by iterators in the ranges [first1, last1) and [first2, last2) and each ordered by operator<, to form a merged sequence of length K beginning at dest, also ordered by operator<. The function then returns dest + K.

The merge occurs without altering the relative order of elements within either sequence. Moreover, for any two elements from different sequences that have equivalent ordering, the element from the ordered range [first1, last1) precedes the other. Thus, the function merges two ordered sequences to form another ordered sequence.

If dest and first1 designate regions of storage, the range [dest, dest + K) must not overlap the range [first1, last1). If dest and first2 designate regions of storage, the range [dest, dest + K) must not overlap the range [first2, last2). The function evaluates the ordering predicate X < Y at most K - 1 times.

The second template function behaves the same, except that it replaces operator<(X, Y) with pred(X, Y).

min

template<class Ty>
    const Ty& min(const Ty& left, const Ty& right);
template<class Ty>
    Ty min(initializer_list<Ty> init); [added with C++11]
template<class Ty, class Pr>
    const Ty& min(const Ty& left, const Ty& right, Pr pred);
template<class Ty, class Pr>
    Ty min(initializer_list<Ty> init, Pr pred); [added with C++11]

The first template function returns right if right < left. Otherwise it returns left. Ty need supply only a single-argument constructor and a destructor.

The second template function returns the leftmost element of init that compares no larger than any other element.

The remaining template functions behaves the same, except that they replace operator<(X, Y) with pred(X, Y).

minmax

template<class Ty>
    pair<const Ty&, const Ty&>
        minmax(const Ty& left, const Ty& right); [added with C++11]
template<class Ty>
    pair<Ty, Ty>
        minmax(initializer_list<Ty> init); [added with C++11]
template<class Ty, class Pr>
    pair<const Ty&, const Ty&>
        minmax(const Ty& left, const Ty& right, Pr pred); [added with C++11]
template<class Ty, class... Types, class Pr>
    pair<Ty, Ty>
        minmax(initializer_list<Ty> init, Pr pred); [added with C++11]

The first template function returns pair<const Ty&, const Ty&>(right, left) if right < left. Otherwise it returns pair<const Ty&, const Ty&>(left, right). The function performs exactly one comparison.

The second template function returns a pair whose first element is the leftmost element of init that compares no larger than any other element, and whose second element is the rightmost element of init that compares no smaller than any other element.

The remaining template functions behaves the same, except that they replace operator<(X, Y) with pred(X, Y).

min_element

template<class FwdIt>
    FwdIt min_element(FwdIt first, FwdIt last);
template<class FwdIt, class Pr>
    FwdIt min_element(FwdIt first, FwdIt last, Pr pred);

The first template function determines the lowest value of N in the range [0, last - first) such that, for each M in the range [0, last - first) the predicate *(first + M) < *(first + N) is false. It then returns first + N. Thus, the function determines the lowest position that contains the smallest value in the sequence.

The function evaluates the ordering predicate X < Y exactly max((last - first) - 1, 0) times.

The second template function behaves the same, except that it replaces operator<(X, Y) with pred(X, Y).

minmax_element

template<class FwdIt>
    pair<FwdIt, FwdIt>
        minmax_element(FwdIt first, FwdIt last); [added with C++11]
template<class FwdIt, class Pr>
    pair<FwdIt, FwdIt>
        minmax_element(FwdIt first, FwdIt last, Pr pred); [added with C++11]

The first template function returns a pair whose first element is the leftmost element of init that compares no larger than any other element, and whose second element is the rightmost element of init that compares no smaller than any other element.

The second template function behaves the same, except that it replaces operator<(X, Y) with pred(X, Y).

If the sequence is non-empty, the function performs at most 3 * (last - first - 1) / 2 comparisons.

mismatch

template<class InIt1, class InIt2>
    pair<InIt1, InIt2> mismatch(InIt1 first1, InIt1 last1,
        InIt2 first2);
template<class InIt1, class InIt2, class Pr>
    pair<InIt1, InIt2> mismatch(InIt1 first1, InIt1 last1,
        InIt2 first2, Pr pred);

The first template function determines the lowest value of N in the range [0, last1 - first1) for which the predicate !(*(first1 + N) == *(first2 + N)) is true. Here, operator== must perform a pairwise comparison between its operands. It then returns pair(first1 + N, first2 + N). If no such value exists, N has the value last1 - first1. The function evaluates the predicate at most once for each N.

The second template function behaves the same, except that the predicate is pred(*(first1 + N), *(first2 + N)).

move

template<class InIt, class OutIt>
    OutIt move(InIt first, InIt last,
        OutIt dest); [added with C++11]

The template function evaluates *(dest + N) = move(*(first + N))) once for each N in the range [0, last - first), for strictly increasing values of N beginning with the lowest value. It then returns dest + N. If dest and first designate regions of storage, dest must not be in the range [first, last).

move_backward

template<class BidIt1, class BidIt2>
    BidIt2 move_backward(BidIt1 first, BidIt1 last,
        BidIt2 dest); [added with C++11]

The template function evaluates *(dest - N - 1) = move(*(last - N - 1))) once for each N in the range [0, last - first), for strictly increasing values of N beginning with the lowest value. It then returns dest - (last - first). If dest and first designate regions of storage, dest must not be in the range (first, last).

next_permutation

template<class BidIt>
    bool next_permutation(BidIt first, BidIt last);
template<class BidIt, class Pr>
    bool next_permutation(BidIt first, BidIt last,
        Pr pred);

The first template function determines a repeating sequence of permutations, whose initial permutation occurs when the sequence designated by iterators in the range [first, last) is ordered by operator<. (The elements are sorted in ascending order.) It then reorders the elements in the sequence, by evaluating swap(X, Y) for the elements X and Y zero or more times, to form the next permutation. The function returns true only if the resulting sequence is not the initial permutation. Otherwise, the resultant sequence is the one next larger lexicographically than the original sequence.

The function evaluates swap(X, Y) at most (last - first) / 2.

The second template function behaves the same, except that it replaces operator<(X, Y) with pred(X, Y).

none_of

template<class InIt, class Pr>
    bool none_of(InIt first, InIt last,
        Pr pred); [added with C++11]

The template function returns true only if, for each N in the range [0, last - first), the predicate pred(*(first + N)) is false.

nth_element

template<class RanIt>
    void nth_element(RanIt first, RanIt nth, RanIt last);
template<class RanIt, class Pr>
    void nth_element(RanIt first, RanIt nth, RanIt last,
        Pr pred);

The first template function reorders the sequence designated by iterators in the range [first, last) such that for each N in the range [0, nth - first) and for each M in the range [nth - first, last - first) the predicate !(*(first + M) < *(first + N)) is true. Moreover, for N equal to nth - first and for each M in the range (nth - first, last - first) the predicate !(*(first + M) < *(first + N)) is true. Thus, if nth != last the element *nth is in its proper position if elements of the entire sequence were sorted in ascending order, ordered by operator<. Any elements before this one belong before it in the sort sequence, and any elements after it belong after it.

The function evaluates the ordering predicate X < Y a number of times proportional to last - first, on average.

The second template function behaves the same, except that it replaces operator<(X, Y) with pred(X, Y).

Beginning with C++11, the affected objects need only be movable, not copyable.

partial_sort

template<class RanIt>
    void partial_sort(RanIt first, RanIt mid,
        RanIt last);
template<class RanIt, class Pr>
    void partial_sort(RanIt first, RanIt mid,
        RanIt last, Pr pred);

The first template function reorders the sequence designated by iterators in the range [first, last) such that for each N in the range [0, mid - first) and for each M in the range (N, last - first) the predicate !(*(first + M) < *(first + N)) is true. Thus, the smallest mid - first elements of the entire sequence are sorted in ascending order, ordered by operator<. The order of the remaining elements is otherwise unspecified.

The function evaluates the ordering predicate X < Y a number of times proportional to at most ceil((last - first) * log(mid - first)).

The second template function behaves the same, except that it replaces operator<(X, Y) with pred(X, Y).

Beginning with C++11, the affected objects need only be movable, not copyable.

partial_sort_copy

template<class InIt, class RanIt>
    RanIt partial_sort_copy(InIt first1, InIt last1,
        RanIt first2, RanIt last2);
template<class InIt, class RanIt, class Pr>
    RanIt partial_sort_copy(InIt first1, InIt last1,
        RanIt first2, RanIt last2, Pr pred);

The first template function determines K, the number of elements to copy as the smaller of last1 - first1 and last2 - first2. It then copies and reorders K elements of the sequence designated by iterators in the range [first1, last1) such that the K elements copied to first2 are ordered by operator<. Moreover, for each N in the range [0, K) and for each M in the range (0, last1 - first1) corresponding to an uncopied element, the predicate !(*(first2 + M) < *(first1 + N)) is true. Thus, the smallest K elements of the entire sequence designated by iterators in the range [first1, last1) are copied and sorted in ascending order to the range [first2, first2 + K).

The function evaluates the ordering predicate X < Y a number of times proportional to at most ceil((last - first) * log(K)).

The second template function behaves the same, except that it replaces operator<(X, Y) with pred(X, Y).

partition

template<class FwdIt, class Pr>
    FwdIt partition(FwdIt first, FwdIt last, Pr pred);

The template function reorders the sequence designated by iterators in the range [first, last) and determines the value K such that for each N in the range [0, K) the predicate pred(*(first + N)) is true, and for each N in the range [K, last - first) the predicate pred(*(first + N)) is false. The function then returns first + K. (The resulting sequence is partitioned by pred.)

The predicate must not alter its operand. The function evaluates pred(*(first + N)) exactly last - first times, and swaps at most (last - first) / 2 pairs of elements.

partition_copy

template<class InIt, class OutIt1, class OutIt2, class Pr>
    pair<OutIt1, OutIt2>
        partition_copy(InIt first, InIt last,
            OutIt1 dest1, OutIt2 dest2, Pr pred); [added with C++11]

The template function copies each element X in [first, last) to *dest1++ if pred(X) is true, otherwise to *dest2++. It returns pair<OutIt1, OutIt2>(dest1, dest2).

partition_point

template<class FwdIt, class Pr>
    FwdIt partition_point(FwdIt first, FwdIt last,
        Pr pred); [added with C++11]

The template function finds the first iterator it in [first, last) for which pred(*it) is false. The sequence must be ordered by pred.

pop_heap

template<class RanIt>
    void pop_heap(RanIt first, RanIt last);
template<class RanIt, class Pr>
    void pop_heap(RanIt first, RanIt last, Pr pred);

The first template function reorders the sequence designated by iterators in the range [first, last) to form a new heap, ordered by operator< and designated by iterators in the range [first, last - 1), leaving the original element at *first subsequently at *(last - 1). The original sequence must designate an existing heap, also ordered by operator<. Thus, first != last must be true and *(last - 1) is the element to remove from (pop off) the heap.

The function evaluates the ordering predicate X < Y at most ceil(2 * log(last - first)) times.

The second template function behaves the same, except that it replaces operator<(X, Y) with pred(X, Y).

Beginning with C++11, the affected objects need only be movable, not copyable.

prev_permutation

template<class BidIt>
    bool prev_permutation(BidIt first, BidIt last);
template<class BidIt, class Pr>
    bool prev_permutation(BidIt first, BidIt last,
        Pr pred);

The first template function determines a repeating sequence of permutations, whose initial permutation occurs when the sequence designated by iterators in the range [first, last) is the reverse of one ordered by operator<. (The elements are sorted in descending order.) It then reorders the elements in the sequence, by evaluating swap(X, Y) for the elements X and Y zero or more times, to form the previous permutation. The function returns true only if the resulting sequence is not the initial permutation. Otherwise, the resultant sequence is the one next smaller lexicographically than the original sequence.

The function evaluates swap(X, Y) at most (last - first) / 2.

The second template function behaves the same, except that it replaces operator<(X, Y) with pred(X, Y).

push_heap

template<class RanIt>
    void push_heap(RanIt first, RanIt last);
template<class RanIt, class Pr>
    void push_heap(RanIt first, RanIt last, Pr pred);

The first template function reorders the sequence designated by iterators in the range [first, last) to form a new heap ordered by operator<. Iterators in the range [first, last - 1) must designate an existing heap, also ordered by operator<. Thus, first != last must be true and *(last - 1) is the element to add to (push on) the heap.

The function evaluates the ordering predicate X < Y at most ceil(log(last - first)) times.

The second template function behaves the same, except that it replaces operator<(X, Y) with pred(X, Y).

Beginning with C++11, the affected objects need only be movable, not copyable.

random_shuffle

template<class RanIt>
    void random_shuffle(RanIt first, RanIt last);
template<class RanIt, class Fn1>
    void random_shuffle(RanIt first, RanIt last,
        Fn1&& func);

The first template function evaluates swap(*(first + N), *(first + M)) once for each N in the range [1, last - first), where M is a value from some uniform random distribution over the range [0, N]. Thus, the function randomly shuffles the order of elements in the sequence.

The function evaluates M and calls swap exactly last - first - 1 times.

The second template function behaves the same, except that M is (Diff)func((Diff)N), where Diff is the type iterator_traits<RanIt>:: difference_type.

Beginning with C++11, func need only be movable, not copyable.

remove

template<class FwdIt, class Ty>
    FwdIt remove(FwdIt first, FwdIt last, const Ty& val);

The template function effectively assigns first to X, then executes the statement:

if (!(*(first + N) == val))
    *X++ = *(first + N);

once for each N in the range [0, last - first). Here, operator== must perform a pairwise comparison between its operands. It then returns X. Thus, the function removes from the resulting sequence all elements for which the predicate *(first + N) == val is true, without altering the relative order of remaining elements, and returns the iterator value that designates the end of the resulting sequence.

Beginning with C++11, the affected objects need only be movable, not copyable.

remove_copy

template<class InIt, class OutIt, class Ty>
    OutIt remove_copy(InIt first, InIt last, OutIt dest,
        const Ty& val);

The template function effectively executes the statement:

if (!(*(first + N) == val))
    *dest++ = *(first + N);

once for each N in the range [0, last - first). Here, operator== must perform a pairwise comparison between its operands. It then returns dest. Thus, the function removes from the resulting sequence all elements for which the predicate *(first + N) == val is true, without altering the relative order of remaining elements, and returns the iterator value that designates the end of the resulting sequence.

If dest and first designate regions of storage, the range [dest, dest + (last - first)) must not overlap the range [first, last).

remove_copy_if

template<class InIt, class OutIt, class Pr>
    OutIt remove_copy_if(InIt first, InIt last, OutIt dest,
        Pr pred);

The template function effectively executes the statement:

if (!pred(*(first + N)))
    *dest++ = *(first + N);

once for each N in the range [0, last - first). It then returns dest. Thus, the function removes from the resulting sequence all elements for which the predicate pred(*(first + N)) is true, without altering the relative order of remaining elements, and returns the iterator value that designates the end of the resulting sequence.

If dest and first designate regions of storage, the range [dest, dest + (last - first)) must not overlap the range [first, last).

remove_if

template<class FwdIt, class Pr>
    FwdIt remove_if(FwdIt first, FwdIt last, Pr pred);

The template function effectively assigns first to X, then executes the statement:

if (!pred(*(first + N)))
    *X++ = *(first + N);

once for each N in the range [0, last - first). It then returns X. Thus, the function removes from the resulting sequence all elements for which the predicate pred(*(first + N)) is true, without altering the relative order of remaining elements, and returns the iterator value that designates the end of the resulting sequence.

Beginning with C++11, the affected objects need only be movable, not copyable.

replace

template<class FwdIt, class Ty>
    void replace(FwdIt first, FwdIt last,
        const Ty& oldval, const Ty& newval);

The template function executes the statement:

if (*(first + N) == oldval)
    *(first + N) = newval;

once for each N in the range [0, last - first). Here, operator== must perform a pairwise comparison between its operands.

replace_copy

template<class InIt, class OutIt, class Ty>
    OutIt replace_copy(InIt first, InIt last, OutIt dest,
        const Ty& oldval, const Ty& newval);

The template function executes the statement:

if (*(first + N) == oldval)
    *(dest + N) = newval;
else
    *(dest + N) = *(first + N)

once for each N in the range [0, last - first). Here, operator== must perform a pairwise comparison between its operands. The function returns the iterator value that designates the end of the resulting sequence.

If dest and first designate regions of storage, the range [dest, dest + (last - first)) must not overlap the range [first, last).

replace_copy_if

template<class InIt, class OutIt, class Pr, class Ty>
    OutIt replace_copy_if(InIt first, InIt last, OutIt dest,
        Pr pred, const Ty& val);

The template function executes the statement:

if (pred(*(first + N)))
    *(dest + N) = val;
else
    *(dest + N) = *(first + N)

once for each N in the range [0, last - first).

If dest and first designate regions of storage, the range [dest, dest + (last - first)) must not overlap the range [first, last). The function returns the iterator value that designates the end of the resulting sequence.

replace_if

template<class FwdIt, class Pr, class Ty>
    void replace_if(FwdIt first, FwdIt last,
        Pr pred, const Ty& val);

The template function executes the statement:

if (pred(*(first + N)))
    *(first + N) = val;

once for each N in the range [0, last - first).

reverse

template<class BidIt>
    void reverse(BidIt first, BidIt last);

The template function evaluates swap(*(first + N), *(last - 1 - N) once for each N in the range [0, (last - first) / 2). Thus, the function reverses the order of elements in the sequence.

Beginning with C++11, the affected objects need only be swappable, not copyable.

reverse_copy

template<class BidIt, class OutIt>
    OutIt reverse_copy(BidIt first, BidIt last, OutIt dest);

The template function evaluates *(dest + N) = *(last - 1 - N) once for each N in the range [0, last - first). It then returns dest + (last - first). Thus, the function reverses the order of elements in the sequence that it copies.

If dest and first designate regions of storage, the range [dest, dest + (last - first)) must not overlap the range [first, last).

rotate

template<class FwdIt>
    FwdIt rotate(FwdIt first, FwdIt mid, FwdIt last);

The template function leaves the value originally stored in *(first + (N + (mid - first)) % (last - first)) subsequently stored in *(first + N) for each N in the range [0, last - first). Thus, if a ``left'' shift by one element leaves the element originally stored in *(first + (N + 1) % (last - first)) subsequently stored in *(first + N), then the function can be said to rotate the sequence either left by mid - first elements or right by last - mid elements. Both [first, mid) and [mid, last) must be valid ranges. The function swaps at most last - first pairs of elements.

Beginning with C++11, the affected objects need only be swappable, not copyable. Also, the function return is no longer void; the function returns first + (last - mid).

rotate_copy

template<class FwdIt, class OutIt>
    OutIt rotate_copy(FwdIt first, FwdIt mid,
        FwdIt last, OutIt dest);

The template function evaluates *(dest + N) = *(first + (N + (mid - first)) % (last - first)) once for each N in the range [0, last - first). Thus, if a ``left'' shift by one element leaves the element originally stored in *(first + (N + 1) % (last - first)) subsequently stored in *(first + N), then the function can be said to rotate the sequence either left by mid - first elements or right by last - mid elements as it copies. Both [first, mid) and [mid, last) must be valid ranges. The function returns the iterator value that designates the end of the resulting sequence.

If dest and first designate regions of storage, the range [dest, dest + (last - first)) must not overlap the range [first, last).

search

template<class FwdIt1, class FwdIt2>
    FwdIt1 search(FwdIt1 first1, FwdIt1 last1,
        FwdIt2 first2, FwdIt2 last2);
template<class FwdIt1, class FwdIt2, class Pr>
    FwdIt1 search(FwdIt1 first1, FwdIt1 last1,
        FwdIt2 first2, FwdIt2 last2, Pr pred);

The first template function determines the lowest value of N in the range [0, (last1 - first1) - (last2 - first2)) such that for each M in the range [0, last2 - first2), the predicate *(first1 + N + M) == *(first2 + M) is true. Here, operator== must perform a pairwise comparison between its operands. It then returns first1 + N. If no such value exists, the function returns last1. It evaluates the predicate at most (last2 - first2) * (last1 - first1) times.

The second template function behaves the same, except that the predicate is pred(*(first1 + N + M), *(first2 + M)).

search_n

template<class FwdIt, class Diff, class Ty>
    FwdIt search_n(FwdIt first, FwdIt last,
        Diff count, const Ty& val);
template<class FwdIt, class Diff, class Ty, class Pr>
    FwdIt search_n(FwdIt first, FwdIt last,
        Diff count, const Ty& val, Pr pred);

The first template function determines the lowest value of N in the range [0, (last - first) - count) such that for each M in the range [0, count), the predicate *(first + N + M) == val is true. Here, operator== must perform a pairwise comparison between its operands. It then returns first + N. If no such value exists, the function returns last. It evaluates the predicate at most last - first times.

The second template function behaves the same, except that the predicate is pred(*(first + N + M), val).

set_difference

template<class InIt1, class InIt2, class OutIt>
    OutIt set_difference(InIt1 first1, InIt1 last1,
        InIt2 first2, InIt2 last2, OutIt dest);
template<class InIt1, class InIt2, class OutIt,
    class Pr>
    OutIt set_difference(InIt1 first1, InIt1 last1,
        InIt2 first2, InIt2 last2, OutIt dest, Pr pred);

The first template function alternately copies values from two sequences designated by iterators in the ranges [first1, last1) and [first2, last2), both ordered by operator<, to form a merged sequence of length K beginning at dest, also ordered by operator<. The function then returns dest + K.

The merge occurs without altering the relative order of elements within either sequence. Moreover, for two elements from different sequences that have equivalent ordering that would otherwise be copied to adjacent elements, the function copies only the element from the ordered range [first1, last1) and skips the other. An element from one sequence that has equivalent ordering with no element from the other sequence is copied from the ordered range [first1, last1) and skipped from the other. Thus, the function merges two ordered sequences to form another ordered sequence that is effectively the difference of two sets.

If dest and first1 designate regions of storage, the range [dest, dest + K) must not overlap the range [first1, last1). If dest and first2 designate regions of storage, the range [dest, dest + K) must not overlap the range [first2, last2). The function evaluates the ordering predicate X < Y at most 2 * ((last1 - first1) + (last2 - first2)) - 1 times.

The second template function behaves the same, except that it replaces operator<(X, Y) with pred(X, Y).

set_intersection

template<class InIt1, class InIt2, class OutIt>
    OutIt set_intersection(InIt1 first1, InIt1 last1,
        InIt2 first2, InIt2 last2, OutIt dest);
template<class InIt1, class InIt2, class OutIt,
    class Pr>
    OutIt set_intersection(InIt1 first1, InIt1 last1,
        InIt2 first2, InIt2 last2, OutIt dest, Pr pred);

The first template function alternately copies values from two sequences designated by iterators in the ranges [first1, last1) and [first2, last2), both ordered by operator<, to form a merged sequence of length K beginning at dest, also ordered by operator<. The function then returns dest + K.

The merge occurs without altering the relative order of elements within either sequence. Moreover, for two elements from different sequences that have equivalent ordering that would otherwise be copied to adjacent elements, the function copies only the element from the ordered range [first1, last1) and skips the other. An element from one sequence that has equivalent ordering with no element from the other sequence is also skipped. Thus, the function merges two ordered sequences to form another ordered sequence that is effectively the intersection of two sets.

If dest and first1 designate regions of storage, the range [dest, dest + K) must not overlap the range [first1, last1). If dest and first2 designate regions of storage, the range [dest, dest + K) must not overlap the range [first2, last2). The function evaluates the ordering predicate X < Y at most 2 * ((last1 - first1) + (last2 - first2)) - 1 times.

The second template function behaves the same, except that it replaces operator<(X, Y) with pred(X, Y).

set_symmetric_difference

template<class InIt1, class InIt2, class OutIt>
    OutIt set_symmetric_difference(InIt1 first1,
        InIt1 last1, InIt2 first2, InIt2 last2, OutIt dest);
template<class InIt1, class InIt2, class OutIt,
    class Pr>
    OutIt set_symmetric_difference(InIt1 first1,
        InIt1 last1, InIt2 first2, InIt2 last2, OutIt dest,
            Pr pred);

The first template function alternately copies values from two sequences designated by iterators in the ranges [first1, last1) and [first2, last2), both ordered by operator<, to form a merged sequence of length K beginning at dest, also ordered by operator<. The function then returns dest + K.

The merge occurs without altering the relative order of elements within either sequence. Moreover, for two elements from different sequences that have equivalent ordering that would otherwise be copied to adjacent elements, the function copies neither element. An element from one sequence that has equivalent ordering with no element from the other sequence is copied. Thus, the function merges two ordered sequences to form another ordered sequence that is effectively the symmetric difference of two sets.

If dest and first1 designate regions of storage, the range [dest, dest + K) must not overlap the range [first1, last1). If dest and first2 designate regions of storage, the range [dest, dest + K) must not overlap the range [first2, last2). The function evaluates the ordering predicate X < Y at most 2 * ((last1 - first1) + (last2 - first2)) - 1 times.

The second template function behaves the same, except that it replaces operator<(X, Y) with pred(X, Y).

set_union

template<class InIt1, class InIt2, class OutIt>
    OutIt set_union(InIt1 first1, InIt1 last1,
        InIt2 first2, InIt2 last2, OutIt dest);
template<class InIt1, class InIt2, class OutIt,
    class Pr>
    OutIt set_union(InIt1 first1, InIt1 last1,
        InIt2 first2, InIt2 last2, OutIt dest, Pr pred);

The first template function alternately copies values from two sequences designated by iterators in the ranges [first1, last1) and [first2, last2), both ordered by operator<, to form a merged sequence of length K beginning at dest, also ordered by operator<. The function then returns dest + K.

The merge occurs without altering the relative order of elements within either sequence. Moreover, for two elements from different sequences that have equivalent ordering that would otherwise be copied to adjacent elements, the function copies only the element from the ordered range [first1, last1) and skips the other. Thus, the function merges two ordered sequences to form another ordered sequence that is effectively the union of two sets.

If dest and first1 designate regions of storage, the range [dest, dest + K) must not overlap the range [first1, last1). If dest and first2 designate regions of storage, the range [dest, dest + K) must not overlap the range [first2, last2). The function evaluates the ordering predicate X < Y at most 2 * ((last1 - first1) + (last2 - first2)) - 1 times.

The second template function behaves the same, except that it replaces operator<(X, Y) with pred(X, Y).

shuffle

template<class RanIt, class Urng>
    void shuffle(RanIt first, RanIt last,
        Urng&& func); [added with C++11]

The template function evaluates swap(*(first + N), *(first + M)) once for each N in the range [1, last - first), where M is (Diff)func(), a random number in the range [0, N). Here, Diff is the type iterator_traits<RanIt>:: difference_type, and func is a uniform random-number generator.

The function evaluates M and calls swap exactly last - first - 1 times.

sort

template<class RanIt>
    void sort(RanIt first, RanIt last);
template<class RanIt, class Pr>
    void sort(RanIt first, RanIt last, Pr pred);

The first template function reorders the sequence designated by iterators in the range [first, last) to form a sequence ordered by operator<. Thus, the elements are sorted in ascending order.

The function evaluates the ordering predicate X < Y a number of times proportional to at most (last - first) * log(last - first).

The second template function behaves the same, except that it replaces operator<(X, Y) with pred(X, Y).

Beginning with C++11, the affected objects need only be movable, not copyable.

sort_heap

template<class RanIt>
    void sort_heap(RanIt first, RanIt last);
template<class RanIt, class Pr>
    void sort_heap(RanIt first, RanIt last, Pr pred);

The first template function reorders the sequence designated by iterators in the range [first, last) to form a sequence that is ordered by operator<. The original sequence must designate a heap, also ordered by operator<. Thus, the elements are sorted in ascending order.

The function evaluates the ordering predicate X < Y at most ceil((last - first) * log(last - first)) times.

The second template function behaves the same, except that it replaces operator<(X, Y) with pred(X, Y).

Beginning with C++11, the affected objects need only be movable, not copyable.

stable_partition

template<class BidIt, class Pr>
    BidIt stable_partition(BidIt first, BidIt last,
        Pr pred);

The template function reorders the sequence designated by iterators in the range [first, last) and determines the value K such that for each N in the range [0, K) the predicate pred(*(first + N)) is true, and for each N in the range [K, last - first) the predicate pred(*(first + N)) is false. It does so without altering the relative order of either the elements designated by indexes in the range [0, K) or the elements designated by indexes in the range [K, last - first). The function then returns first + K.

The predicate must not alter its operand. The function evaluates pred(*(first + N)) exactly last - first times, and swaps at most ceil((last - first) * log(last - first)) pairs of elements. (Given enough temporary storage, it can replace the swaps with at most 2 * (last - first) assignments.)

Beginning with C++11, the affected objects need only be movable, not copyable.

stable_sort

template<class BidIt>
    void stable_sort(BidIt first, BidIt last);
template<class BidIt, class Pr>
    void stable_sort(BidIt first, BidIt last, Pr pred);

The first template function reorders the sequence designated by iterators in the range [first, last) to form a sequence ordered by operator<. It does so without altering the relative order of elements that have equivalent ordering. Thus, the elements are sorted in ascending order.

The function evaluates the ordering predicate X < Y a number of times proportional to at most ceil((last - first) * (log(last - first))^2). (Given enough temporary storage, it can evaluate the predicate a number of times proportional to at most ceil((last - first) * log(last - first)).

The second template function behaves the same, except that it replaces operator<(X, Y) with pred(X, Y).

Beginning with C++11, the affected objects need only be movable, not copyable.

swap

template<class Ty>
    void swap(Ty& left, Ty& right);
template<class Ty, size_t N>
    void swap(Ty (&left)[N], Ty (&right)[N]); [added with C++11]

The template function leaves the value originally stored in right subsequently stored in left, and the value originally stored in left subsequently stored in right. Note that these template functions are also declared in <utility>.

swap_ranges

template<class FwdIt1, class FwdIt2>
    FwdIt2 swap_ranges(FwdIt1 first1, FwdIt1 last1,
        FwdIt2 first2);

The template function evaluates swap(*(first1 + N), *(first2 + N)) once for each N in the range [0, last1 - first1). It then returns first2 + (last1 - first1). If first2 and first1 designate regions of storage, the range [first2, first2 + (last1 - first1)) must not overlap the range [first1, last1).

transform

template<class InIt, class OutIt, class Fn1>
    OutIt transform(InIt first, InIt last, OutIt dest,
        Fn1 func);
template<class InIt1, class InIt2, class OutIt,
    class Fn2>
    OutIt transform(InIt1 first1, InIt1 last1,
        InIt2 first2, OutIt dest, Fn2 func);

The first template function evaluates *(dest + N) = func(*(first + N)) once for each N in the range [0, last - first). It then returns dest + (last - first). The call func(*(first + N)) must not alter *(first + N).

The second template function evaluates *(dest + N) = func(*(first1 + N), *(first2 + N)) once for each N in the range [0, last1 - first1). It then returns dest + (last1 - first1). The call func(*(first1 + N), *(first2 + N)) must not alter either *(first1 + N) or *(first2 + N).

unique

template<class FwdIt>
    FwdIt unique(FwdIt first, FwdIt last);
template<class FwdIt, class Pr>
    FwdIt unique(FwdIt first, FwdIt last, Pr pred);

The first template function effectively assigns first to X, then executes the statement:

if (!(*X == *(first + N + 1)))
    *++X = *(first + N + 1);

once for each N in the range [1, last - first). It then returns X. Thus, the function repeatedly removes from the resulting sequence the second of a pair of elements for which the predicate *(first + N) == *(first + N + 1) is true, until only the first of a sequence of elements survives that satisfies the comparison. Here, operator== must perform a pairwise comparison between its operands. It does so without altering the relative order of remaining elements, and returns the iterator value that designates the end of the resulting sequence. For a non-empty sequence, the function evaluates the predicate last - first - 1 times.

The second template function behaves the same, except that it executes the statement:

if (!pred(*(first + N), *(first + N + 1)))
    *++X = *(first + N + 1);

Note that for a sequence designated by the range [first, last) and ordered by pred, you can remove all but the first of a sequence of elements that have equivalent ordering by calling unique(first, last, not2(pred)).

Beginning with C++11, the affected objects need only be movable, not copyable.

unique_copy

template<class InIt, class OutIt>
    OutIt unique_copy(InIt first, InIt last, OutIt dest);
template<class InIt, class OutIt, class Pr>
    OutIt unique_copy(InIt first, InIt last, OutIt dest,
        Pr pred);

The first template function effectively executes the statement:

if (N == 0 || !(*(first + N - 1) == *(first + N)))
    *dest++ = *(first + N);

once for each N in the range [0, last - first). It then returns dest. Thus, the function repeatedly removes from the resulting sequence the second of a pair of elements for which the predicate *(first + N) == *(first + N - 1) is true, until only the first of a sequence of equal elements survives. Here, operator== must perform a pairwise comparison between its operands. It does so without altering the relative order of remaining elements, and returns the iterator value that designates the end of the copied sequence. For a non-empty sequence, the function evaluates the predicate last - first - 1 times.

If dest and first designate regions of storage, the range [dest, dest + (last - first)) must not overlap the range [first, last).

The second template function behaves the same, except that it executes the statement:

if (N == 0 || !pred(*(first + N - 1), *(first + N)))
    *dest++ = *(first + N);

upper_bound

template<class FwdIt, class Ty>
    FwdIt upper_bound(FwdIt first, FwdIt last,
        const Ty& val);
template<class FwdIt, class Ty, class Pr>
    FwdIt upper_bound(FwdIt first, FwdIt last,
        const Ty& val, Pr pred);

The first template function determines the highest value of N in the range (0, last - first] such that, for each M in the range [0, N) the predicate !(val < *(first + M)) is true, where the elements designated by iterators in the range [first, last) form a sequence partitioned by operator<. It then returns first + N. Thus, the function determines the highest position before which val can be inserted in the sequence and still preserve its ordering.

The function evaluates the ordering predicate X < Y at most ceil(log(last - first)) + 1 times.

The second template function behaves the same, except that it replaces operator<(X, Y) with pred(X, Y).


See also the Table of Contents and the Index.

Copyright © 1992-2013 by P.J. Plauger. Portions derived from work copyright © 1994 by Hewlett-Packard Company. All rights reserved.