Apache C++ Standard Library Reference Guide

## <algorithm>

Library: Algorithms

    Header

- Local Index
- Summary
- Synopsis
- See Also
- Standards Conformance

### Local Index

No Entries

### Summary

The header `<algorithm>` represents the Algorithms library of the C++ Standard Library.

### Synopsis

    namespace std {
      //  lib.alg.nonmodifying, nonmodifying sequence operations:
      template<class InputIterator, class Function>
      Function for_each(InputIterator start, InputIterator finish,
                        Function f);

      template<class InputIterator, class T>
      InputIterator find(InputIterator start,                      InputIterator finish,
                         const T& value);

      template<class InputIterator, class Predicate>
      InputIterator find_if(InputIterator start,
                            InputIterator finish,
                            Predicate pred);

      template<class ForwardIterator1, class ForwardIterator2>
      ForwardIterator1
      find_end(ForwardIterator1 start1, ForwardIterator1 finish1,
               ForwardIterator2 start2, ForwardIterator2 finish2);

      template<class ForwardIterator1, class ForwardIterator2,
               class BinaryPredicate>
      ForwardIterator1
      find_end(ForwardIterator1 start1, ForwardIterator1 finish1,
               ForwardIterator2 start2, ForwardIterator2 finish2,
               BinaryPredicate binary_pred);

      template<class ForwardIterator1, class ForwardIterator2>
      ForwardIterator1
      find_first_of(ForwardIterator1 start1,
                    ForwardIterator1 finish1,
                    ForwardIterator2 start2,
                    ForwardIterator2 finish2);

      template<class ForwardIterator1, class ForwardIterator2,
      class BinaryPredicate>
      ForwardIterator1
      find_first_of(ForwardIterator1 start1,
                    ForwardIterator1 finish1,
                    ForwardIterator2 start2,
                    ForwardIterator2 finish2,
                    BinaryPredicate binary_pred);

      template<class ForwardIterator>
      ForwardIterator adjacent_find(ForwardIterator start,
                                    ForwardIterator finish);

      template<class ForwardIterator, class BinaryPredicate>
      ForwardIterator adjacent_find(ForwardIterator start,
                                    ForwardIterator finish,
                                    BinaryPredicate binary_pred);

      template<class InputIterator, class T>
      typename iterator_traits<InputIterator>::difference_type
      count(InputIterator start, InputIterator finish,
            const T& value);

      template<class InputIterator, class Predicate>
      typename iterator_traits<InputIterator>::difference_type
      count_if(InputIterator start, InputIterator finish,
               Predicate pred);

      template<class InputIterator1, class InputIterator2>
      pair<InputIterator1, InputIterator2>
      mismatch(InputIterator1 start1, InputIterator1 finish1,
               InputIterator2 start2);

      template<class InputIterator1, class InputIterator2,
               class BinaryPredicate>
      pair<InputIterator1, InputIterator2>
      mismatch(InputIterator1 start1, InputIterator1 finish1,
               InputIterator2 start2,             BinaryPredicate binary_pred);

      template<class InputIterator1, class InputIterator2>
      bool equal(InputIterator1 start1, InputIterator1 finish1,
                 InputIterator2 start2);

      template<class InputIterator1, class InputIterator2,
               class BinaryPredicate>
      bool equal(InputIterator1 start1, InputIterator1 finish1,
                 InputIterator2 start2,              BinaryPredicate binary_pred);

      template<class ForwardIterator1, class ForwardIterator2>
      ForwardIterator1 search(ForwardIterator1 start1,
                              ForwardIterator1 finish1,
                              ForwardIterator2 start2,
                              ForwardIterator2 finish2);

      template<class ForwardIterator1, class ForwardIterator2,
      class BinaryPredicate>
      ForwardIterator1 search(ForwardIterator1 start1,
                              ForwardIterator1 finish1,
                              ForwardIterator2 start2,
                              ForwardIterator2 finish2,
                              BinaryPredicate binary_pred);

      template<class ForwardIterator, class Size, class T>
      ForwardIterator  search_n(ForwardIterator start,
                                ForwardIterator finish,
                                Size count,
                                const T& value);

      template<class ForwardIterator, class Size, class T,
               class BinaryPredicate>
      ForwardIterator1 search_n(ForwardIterator start,
                                ForwardIterator finish,
                                Size count,
                                const T& value,
                                BinaryPredicate binary_pred);

      //  lib.alg.modifying.operations,
      //  modifying sequence operations:
      //  lib.alg.copy, copy:
      template<class InputIterator, class OutputIterator>
      OutputIterator copy(InputIterator start,                       InputIterator finish,
                          OutputIterator result);

      template<class BidirectionalIterator1,
               class BidirectionalIterator2>
      BidirectionalIterator2
      copy_backward(BidirectionalIterator1 start,
                    BidirectionalIterator1 finish,
                    BidirectionalIterator2 result);

      //  lib.alg.swap, swap:
      template<class T> void swap(T& a, T& b);

      template<class ForwardIterator1, class ForwardIterator2>
      ForwardIterator2 swap_ranges(ForwardIterator1 start1,
                                   ForwardIterator1 finish1,
                                   ForwardIterator2 start2);

      template<class ForwardIterator1, class ForwardIterator2>
      void iter_swap(ForwardIterator1 a, ForwardIterator2 b);

      template<class InputIterator, class OutputIterator,
               class UnaryOperation>
      OutputIterator transform(InputIterator start,
                               InputIterator finish,
                               OutputIterator result,
                               UnaryOperation op);

      template<class InputIterator1,
               class InputIterator2,
               class OutputIterator,
               class BinaryOperation>
      OutputIterator transform(InputIterator1 start1,
                               InputIterator1 finish1,
                               InputIterator2 start2,
                               OutputIterator result,
                               BinaryOperation binary_op);

      template<class ForwardIterator, class T>
      void replace(ForwardIterator start, ForwardIterator finish,
                   const T& old_value, const T& new_value);

      template<class ForwardIterator, class Predicate, class T>
      void replace_if(ForwardIterator start,                   ForwardIterator finish,
                      Predicate pred, const T& new_value);

      template<class InputIterator, class OutputIterator, class T>
      OutputIterator replace_copy(InputIterator start,
                                  InputIterator finish,
                                  OutputIterator result,
                                  const T& old_value,
                                  const T& new_value);

      template<class Iterator, class OutputIterator,
               class Predicate, class T>
      OutputIterator replace_copy_if(Iterator start,
                                     Iterator finish,
                                     OutputIterator result,
                                     Predicate pred,
                                     const T& new_value);

      template<class ForwardIterator, class T>
      void fill(ForwardIterator start, ForwardIterator finish,
                const T& value);

      template<class OutputIterator, class Size, class T>
      void fill_n(OutputIterator start, Size n, const T& value);

      template<class ForwardIterator, class Generator>
      void generate(ForwardIterator start, ForwardIterator finish,
                    Generator gen);

      template<class OutputIterator, class Size, class Generator>
      void generate_n(OutputIterator start, Size n,
                      Generator gen);

      template<class ForwardIterator, class T>
      ForwardIterator remove(ForwardIterator start,
                             ForwardIterator finish,
                             const T& value);

      template<class ForwardIterator, class Predicate>
      ForwardIterator remove_if(ForwardIterator start,
                                ForwardIterator finish,
                                Predicate pred);

      template<class InputIterator, class OutputIterator, class T>
      OutputIterator remove_copy(InputIterator start,
                                 InputIterator finish,
                                 OutputIterator result,
                                 const T& value);

      template<class InputIterator, class OutputIterator,
               class Predicate>
      OutputIterator remove_copy_if(InputIterator start,
                                    InputIterator finish,
                                    OutputIterator result,
                                    Predicate pred);

      template<class ForwardIterator>
      ForwardIterator unique(ForwardIterator start,
                             ForwardIterator finish);

      template<class ForwardIterator, class BinaryPredicate>
      ForwardIterator unique(ForwardIterator start,
                             ForwardIterator finish,
                             BinaryPredicate binary_pred);

      template<class InputIterator, class OutputIterator>
      OutputIterator unique_copy(InputIterator start,
                                 InputIterator finish,
                                 OutputIterator result);

      template<class InputIterator, class OutputIterator,
               class BinaryPredicate>
      OutputIterator unique_copy(InputIterator start,
                                 InputIterator finish,
                                 OutputIterator result,
                                 BinaryPredicate binary_pred);

      template<class BidirectionalIterator>
      void reverse(BidirectionalIterator start,
                   BidirectionalIterator finish);

      template<class BidirectionalIterator, class OutputIterator>
      OutputIterator reverse_copy(BidirectionalIterator start,
                                  BidirectionalIterator finish,
                                  OutputIterator result);

      template<class ForwardIterator>
      void rotate(ForwardIterator start, ForwardIterator mid,
                  ForwardIterator finish);

      template<class ForwardIterator, class OutputIterator>
      OutputIterator rotate_copy(ForwardIterator start,
                                 ForwardIterator mid,
                                 ForwardIterator finish,
                                 OutputIterator result);

      template<class RandomAccessIterator>
      void random_shuffle(RandomAccessIterator start,
                          RandomAccessIterator finish);

      template<class RandomAccessIterator,
               class RandomNumberGenerator>
      void random_shuffle(RandomAccessIterator start,
                          RandomAccessIterator finish,
                          RandomNumberGenerator& rand);

      //  lib.alg.partitions, partitions:
      template<class BidirectionalIterator, class Predicate>
      BidirectionalIterator partition(BidirectionalIterator start,
                                      BidirectionalIterator finish,
                                      Predicate pred);

      template<class BidirectionalIterator, class Predicate>
      BidirectionalIterator
      stable_partition(BidirectionalIterator start,
                       BidirectionalIterator finish,
                       Predicate pred);

      //  lib.alg.sorting, sorting and related operations:
      //  lib.alg.sort, sorting:
      template<class RandomAccessIterator>
      void sort(RandomAccessIterator start,
                RandomAccessIterator finish);

      template<class RandomAccessIterator, class Compare>
      void sort(RandomAccessIterator start,
                RandomAccessIterator finish,
                Compare comp);

      template<class RandomAccessIterator>
      void stable_sort(RandomAccessIterator start,
                       RandomAccessIterator finish);

      template<class RandomAccessIterator, class Compare>
      void stable_sort(RandomAccessIterator start,
                       RandomAccessIterator finish,
                       Compare comp);

      template<class RandomAccessIterator>
      void partial_sort(RandomAccessIterator start,
                        RandomAccessIterator mid,
                        RandomAccessIterator finish);

      template<class RandomAccessIterator, class Compare>
      void partial_sort(RandomAccessIterator start,
                        RandomAccessIterator mid,
                        RandomAccessIterator finish,
                        Compare comp);

      template<class InputIterator, class RandomAccessIterator>
      RandomAccessIterator partial_sort_copy(InputIterator start,
                       InputIterator finish,
                       RandomAccessIterator result_start,
                       RandomAccessIterator result_finish);

      template<class InputIterator, class RandomAccessIterator,
               class Compare>
      RandomAccessIterator partial_sort_copy(InputIterator start,
                       InputIterator finish,
                       RandomAccessIterator result_start,
                       RandomAccessIterator result_finish,
                       Compare comp);

      template<class RandomAccessIterator>
      void nth_element(RandomAccessIterator start,
                       RandomAccessIterator nth,
                       RandomAccessIterator finish);

      template<class RandomAccessIterator, class Compare>
      void nth_element(RandomAccessIterator start,
                       RandomAccessIterator nth,
                       RandomAccessIterator finish,
                       Compare comp);

      //  lib.alg.binary.search, binary search:
      template<class ForwardIterator, class T>
      ForwardIterator lower_bound(ForwardIterator start,
                                  ForwardIterator finish,
                                  const T& value);

      template<class ForwardIterator, class T, class Compare>
      ForwardIterator lower_bound(ForwardIterator start,
                                  ForwardIterator finish,
                                  const T& value,
                                  Compare comp);

      template<class ForwardIterator, class T>
      ForwardIterator upper_bound(ForwardIterator start,
                                  ForwardIterator finish,
                                  const T& value);

      template<class ForwardIterator, class T, class Compare>
      ForwardIterator upper_bound(ForwardIterator start,
                                  ForwardIterator finish,
                                  const T& value,
                                  Compare comp);

      template<class ForwardIterator, class T>
      pair<ForwardIterator, ForwardIterator>
      equal_range(ForwardIterator start, ForwardIterator finish,
                  const T& value);

      template<class ForwardIterator, class T, class Compare>
      pair<ForwardIterator, ForwardIterator>
      equal_range(ForwardIterator start, ForwardIterator finish,
                  const T& value, Compare comp);

      template<class ForwardIterator, class T>
      bool binary_search(ForwardIterator start,
                         ForwardIterator finish,
                         const T& value);

      template<class ForwardIterator, class T, class Compare>
      bool binary_search(ForwardIterator start,
                        ForwardIterator finish,
                        const T& value,
                        Compare comp);

      //  lib.alg.merge, merge:
      template<class InputIterator1, class InputIterator2,
               class OutputIterator>
      OutputIterator merge(InputIterator1 start1,
                           InputIterator1 finish1,
                           InputIterator2 start2,
                           InputIterator2 finish2,
                           OutputIterator result);

      template<class InputIterator1, class InputIterator2,
               class OutputIterator, class Compare>
      OutputIterator merge(InputIterator1 start1,
                           InputIterator1 finish1,
                           InputIterator2 start2,
                           InputIterator2 finish2,
                           OutputIterator result,
                           Compare comp);

      template<class BidirectionalIterator>
      void inplace_merge(BidirectionalIterator start,
                         BidirectionalIterator mid,
                         BidirectionalIterator finish);

      template<class BidirectionalIterator, class Compare>
      void inplace_merge(BidirectionalIterator start,
                         BidirectionalIterator mid,
                         BidirectionalIterator finish,
                         Compare comp);

      //  lib.alg.set.operations, set operations:
      template<class InputIterator1, class InputIterator2>
      bool includes(InputIterator1 start1, InputIterator1 finish1,
                    InputIterator2 start2, InputIterator2 finish2);

      template<class InputIterator1, class InputIterator2,
               class Compare>
      bool includes(InputIterator1 start1,
                    InputIterator1 finish1,
                    InputIterator2 start2,
                    InputIterator2 finish2,
                    Compare comp);

      template<class InputIterator1, class InputIterator2,
               class OutputIterator>
      OutputIterator set_union(InputIterator1 start1,
                               InputIterator1 finish1,
                               InputIterator2 start2,
                               InputIterator2 finish2,
                               OutputIterator result);

      template<class InputIterator1, class InputIterator2,
               class OutputIterator, class Compare>
      OutputIterator set_union(InputIterator1 start1,
                               InputIterator1 finish1,
                               InputIterator2 start2,
                               InputIterator2 finish2,
                               OutputIterator result,
                               Compare comp);

      template<class InputIterator1, class InputIterator2,
               class OutputIterator>
      OutputIterator set_intersection(InputIterator1 start1,
                                      InputIterator1 finish1,
                                      InputIterator2 start2,
                                      InputIterator2 finish2,
                                      OutputIterator result);

      template<class InputIterator1, class InputIterator2,
               class OutputIterator, class Compare>
      OutputIterator set_intersection(InputIterator1 start1,
                                      InputIterator1 finish1,
                                      InputIterator2 start2,
                                      InputIterator2 finish2,
                                      OutputIterator result,
                                      Compare comp);

      template<class InputIterator1, class InputIterator2,
               class OutputIterator>
      OutputIterator set_difference(InputIterator1 start1,
                                    InputIterator1 finish1,
                                    InputIterator2 start2,
                                    InputIterator2 finish2,
                                    OutputIterator result);

      template<class InputIterator1, class InputIterator2,
               class OutputIterator, class Compare>
      OutputIterator set_difference(InputIterator1 start1,
                                    InputIterator1 finish1,
                                    InputIterator2 start2,
                                    InputIterator2 finish2,
                                    OutputIterator result,
                                    Compare comp);

      template<class InputIterator1, class InputIterator2,
               class OutputIterator>
      OutputIterator set_symmetric_difference(
                             InputIterator1 start1,
                             InputIterator1 finish1,
                             InputIterator2 start2,
                             InputIterator2 finish2,
                             OutputIterator result);

      template<class InputIterator1, class InputIterator2,
               class OutputIterator, class Compare>
      OutputIterator set_symmetric_difference(
                             InputIterator1 start1,
                             InputIterator1 finish1,
                             InputIterator2 start2,
                             InputIterator2 finish2,
                             OutputIterator result,
                             Compare comp);

      //  lib.alg.heap.operations, heap operations:
      template<class RandomAccessIterator>
      void push_heap(RandomAccessIterator start,
                     RandomAccessIterator finish);

      template<class RandomAccessIterator, class Compare>
      void push_heap(RandomAccessIterator start,
                     RandomAccessIterator finish,
                     Compare comp);

      template<class RandomAccessIterator>
      void pop_heap(RandomAccessIterator start,
                    RandomAccessIterator finish);

      template<class RandomAccessIterator, class Compare>
      void pop_heap(RandomAccessIterator start,
                    RandomAccessIterator finish,
                    Compare comp);

      template<class RandomAccessIterator>
      void make_heap(RandomAccessIterator start,
                     RandomAccessIterator finish);

      template<class RandomAccessIterator, class Compare>
      void make_heap(RandomAccessIterator start,
                     RandomAccessIterator finish,
                     Compare comp);

      template<class RandomAccessIterator>
      void sort_heap(RandomAccessIterator start,
                     RandomAccessIterator finish);

      template<class RandomAccessIterator, class Compare>
      void sort_heap(RandomAccessIterator start,
                     RandomAccessIterator finish,
                     Compare comp);

      //  lib.alg.min.max, minimum and maximum:
      template<class T> const T& min(const T& a, const T& b);

      template<class T, class Compare>
      const T& min(const T& a, const T& b, Compare comp);

      template<class T> const T& max(const T& a, const T& b);

      template<class T, class Compare>
      const T& max(const T& a, const T& b, Compare comp);

      template<class ForwardIterator>
      ForwardIterator min_element(ForwardIterator start,
                                  ForwardIterator finish);

      template<class ForwardIterator, class Compare>
      ForwardIterator min_element(ForwardIterator start,
                                  ForwardIterator finish,
                                  Compare comp);

      template<class ForwardIterator>
      ForwardIterator max_element(ForwardIterator start,
                                  ForwardIterator finish);

      template<class ForwardIterator, class Compare>
      ForwardIterator max_element(ForwardIterator start,
                                  ForwardIterator finish,
                                  Compare comp);

      template<class InputIterator1, class InputIterator2>
      bool lexicographical_compare(InputIterator1 start1,
                                   InputIterator1 finish1,
                                   InputIterator2 start2,
                                   InputIterator2 finish2);

      template<class InputIterator1, class InputIterator2,
               class Compare>
      bool lexicographical_compare(InputIterator1 start1,
                                   InputIterator1 finish1,
                                   InputIterator2 start2,
                                   InputIterator2 finish2,
                                   Compare comp);

      //  lib.alg.permutation.generators, permutations
      template<class BidirectionalIterator>
      bool next_permutation(BidirectionalIterator start,
                            BidirectionalIterator finish);

      template<class BidirectionalIterator, class Compare>
      bool next_permutation(BidirectionalIterator start,
                            BidirectionalIterator finish,
                            Compare comp);

      template<class BidirectionalIterator>
      bool prev_permutation(BidirectionalIterator start,
                            BidirectionalIterator finish);

      template<class BidirectionalIterator, class Compare>
      bool prev_permutation(BidirectionalIterator start,
                            BidirectionalIterator finish,
                            Compare comp);
    }

### See Also

Algorithms, `for_each()`, `find()`, `find_if()`, `find_end()`, `find_first_of()`, `adjacent_find()`, `count()`, `count_if()`, `mismatch()`, `equal()`, `search()`, `search_n()`, `copy()`, `copy_backward()`, `swap()`, `swap_ranges()`, `iter_swap()`, `transform()`, `replace()`, `replace_if()`, `replace_copy()`, `replace_copy_if()`, `fill()`, `fill_n()`, `generate()`, `generate_n()`, `remove()`, `remove_if()`, `remove_copy()`, `remove_copy_if()`, `unique()`, `unique_copy()`, `reverse()`, `reverse_copy()`, `rotate()`, `rotate_copy()`, `random_shuffle()`, `partition()`, `stable_partition()`, `sort()`, `partial_sort()`, `partial_sort_copy()`, `nth_element()`, `lower_bound()`, `upper_bound()`, `equal_range()`, `binary_search()`, `merge()`, `inplace_merge()`, `includes()`, `set_union()`, `set_intersection()`, `set_difference()`, `set_symmetric_difference()`, `push_heap()`, `pop_heap()`, `make_heap()`, `sort_heap()`, `min()`, `max()`, `min_element()`, `max_element()`, `lexicographical_compare()`, `next_permutation()`, `prev_permutation()`

### Standards Conformance

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