4. Header : Algorithm

I. Overview

  • The header defines a collection of functions especially designed to be used on ranges of elements.
  • A range is any sequence of objects that can be accessed through iterators or pointers, such as an array or an instance of some of the STL containers.

II. Non-modifying sequence operations

FunctionIntro
all_ofreturn true if all elements in range fulfills condition or empty range
any_ofreturn true if one element existing in range fulfills condition
none_ofreturn true if all elements in range don’t fulfill condition or empty range
for_eachApply function to range
findFind value in range
find_ifFind element in range
find_if_notFind element in range (negative condition)
find_endFind last subsequence in range
find_first_ofFind element from set in range
adjacent_findFind equal adjacent elements in range
countCount appearances of value in range
count (InputIterator first, InputIterator last, const T& val);
count_ifReturn number of elements in range satisfying condition UnaryPredicate pred
mismatchReturn first position where two ranges differ
equalTest whether the elements in two ranges are equal
is_permutationreturn true if elements in two range is same without ignring same element, even in a different order.
searchSearch range for the subsequence appearing firstly, another version of find_end()
search_nSearch range for a continuous sequence in which all elements fulfuling condition
all_of
  • Prototype
    template <class InputIterator, class UnaryPredicate>
    bool all_of (InputIterator first, InputIterator last, UnaryPredicate pred);
  • Details
    • returns true if pred returns true for all the elements in the range [first,last) or if the range is empty,
    • returns false otherwise
    • parameter pred : Unary function
for_each
  • Prototype
    template <class InputIterator, class Function>
    Function for_each (InputIterator first, InputIterator last, Function fn);
  • Details
    • returns fn
    • parameter fn : Unary function
    • fn using deference parameter can modify elements in the sequence
find
  • Prototype
    template <class InputIterator, class T>
    InputIterator find (InputIterator first, InputIterator last, const T& val);
 template<class InputIterator, class T>
  InputIterator find (InputIterator first, InputIterator last, const T& val)
{
  while (first!=last) {
    if (*first==val) return first;
    ++first;
  }
  return last;
}
  • Details
    • Returns an iterator to the first element in the range [first,last) that compares equal to val
    • Returns last if no such element is found
find_if
  • Prototype
    template <class InputIterator, class UnaryPredicate>
    InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred);
  • Details
    • Returns an iterator to the first element in the range [first,last) for which pred returns true.
    • Returns last if no such element is found
find_end
  • Prototype
    • equality (1)
template <class ForwardIterator1, class ForwardIterator2>`
ForwardIterator1 find_end (ForwardIterator1 first1, ForwardIterator1 last1,
                           ForwardIterator2 first2, ForwardIterator2 last2);
  • predicate (2)
template <class ForwardIterator1, class ForwardIterator2>`
ForwardIterator1 find_end (ForwardIterator1 first1, ForwardIterator1 last1,
                           ForwardIterator2 first2, ForwardIterator2 last2,
                           BinaryPredicate pred);
  • Details
    • Searches the range [first1,last1) for the last occurrence of the sequence defined by [first2,last2), and returns an iterator to its first element, or last1 if no occurrences are found.
    • The elements in both ranges are compared sequentially using operator== (or pred, in version (2))
find_end
  • Prototype
    + equality (1)
template <class ForwardIterator1, class ForwardIterator2>
   ForwardIterator1 find_first_of (ForwardIterator1 first1, ForwardIterator1 last1,
                                   ForwardIterator2 first2, ForwardIterator2 last2);
  • predicate (2)
template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
   ForwardIterator1 find_first_of (ForwardIterator1 first1, ForwardIterator1 last1,
                                   ForwardIterator2 first2, ForwardIterator2 last2,
                                   BinaryPredicate pred);
  • Details
    • Returns an iterator to the first element in the range [first1,last1) that matches any of the elements in [first2,last2). If no such element is found, the function returns last1.
    • The elements in [first1,last1) are sequentially compared to each of the values in [first2,last2) using operator== (or pred, in version (2)), until a pair matches.
adjacent_find
  • Prototype
    + equality (1)
template <class ForwardIterator>
   ForwardIterator adjacent_find (ForwardIterator first, ForwardIterator last);
  • predicate (2)
template <class ForwardIterator, class BinaryPredicate>
   ForwardIterator adjacent_find (ForwardIterator first, ForwardIterator last,
                                  BinaryPredicate pred);
  • Details
    • Searches the range [first,last) for the first occurrence of two consecutive elements that match, and returns an iterator to the first of these two elements, or last if no such pair is found.
    • Two elements match if they compare equal using operator== (or using pred, in version (2)).
mismatch
  • Prototype
    + equality (1)
template <class InputIterator1, class InputIterator2>
  pair<InputIterator1, InputIterator2>
    mismatch (InputIterator1 first1, InputIterator1 last1,
              InputIterator2 first2);
  • predicate (2)
template <class InputIterator1, class InputIterator2, class BinaryPredicate>
  pair<InputIterator1, InputIterator2>
    mismatch (InputIterator1 first1, InputIterator1 last1,
              InputIterator2 first2, BinaryPredicate pred);
  • Details
    • Compares the elements in the range [first1,last1) with those in the range beginning at first2, and returns the first element of both sequences that does not match.
    • The function returns a pair of iterators to the first element in each range that does not match.
equal
  • Prototype
    + equality (1)
template <class InputIterator1, class InputIterator2>
  bool equal (InputIterator1 first1, InputIterator1 last1,
              InputIterator2 first2);
  • predicate (2)
template <class InputIterator1, class InputIterator2, class BinaryPredicate>
  bool equal (InputIterator1 first1, InputIterator1 last1,
              InputIterator2 first2, BinaryPredicate pred);
  • Details
    • Compares the elements in the range [first1,last1) with those in the range beginning at first2, and returns true if all of the elements in both ranges match.
    • The elements are compared using operator== (or pred, in version (2)).
search_n
  • Prototype
    + equality (1)
template <class ForwardIterator, class Size, class T>
   ForwardIterator search_n (ForwardIterator first, ForwardIterator last,
                             Size count, const T& val);
  • predicate (2)
template <class ForwardIterator, class Size, class T, class BinaryPredicate>
   ForwardIterator search_n ( ForwardIterator first, ForwardIterator last,
                              Size count, const T& val, BinaryPredicate pred );
  • Details
    • Searches the range [first,last) for a sequence of count elements, each comparing equal to val (or for which pred returns true).

III. Modifying sequence operations

FunctionIntro
moveMove range of elements
OutputIterator move (InputIterator first, InputIterator last, OutputIterator result);
Returns an iterator to the end of the destination
move_backwardMove range of elements into the range terminating at result
BidirectionalIterator2 move_backward (BidirectionalIterator1 first, BidirectionalIterator1 last, BidirectionalIterator2 result);
Returns an iterator to the first element of the destination
replaceAssigns new_value to all the elements in the range [first,last) that compare equal to old_value.
void replace (ForwardIterator first, ForwardIterator last, const T& old_value, const T& new_value);
replace_if‘void replace_if (ForwardIterator first, ForwardIterator last, UnaryPredicate pred, const T& new_value );’
replace_copyreplace and return an iterator to the element that follows the last element written in the result sequence.
OutputIterator replace_copy (InputIterator first, InputIterator last, OutputIterator result, const T& old_value, const T& new_value);
replace_copy_if
transformTransform range
removeReturns an iterator to the element that follows the last element not removed.
ForwardIterator remove (ForwardIterator first, ForwardIterator last, const T& val);
remove_ifForwardIterator remove_if (ForwardIterator first, ForwardIterator last, UnaryPredicate pred);
reversevoid reverse (BidirectionalIterator first, BidirectionalIterator last);
rotaterotate left distance(first, middle)
void rotate (ForwardIterator first, ForwardIterator middle, ForwardIterator last);
transform
  • Prototype
    • unary operation(1)
OutputIterator transform (InputIterator first1, InputIterator last1,
                            OutputIterator result, UnaryOperation op);
  • binary operation(2)
OutputIterator transform (InputIterator1 first1, InputIterator1 last1,
                            InputIterator2 first2, OutputIterator result,
                            BinaryOperation binary_op);
  • Details
    • (1) unary operation
      Applies op to each of the elements in the range [first1,last1) and stores the value returned by each operation in the range that begins at result.
    • (2) binary operation
      Calls binary_op using each of the elements in the range [first1,last1) as first argument, and the respective argument in the range that begins at first2 as second argument. The value returned by each call is stored in the range that begins at result.

IV. Partitions

V. Sorting

FunctionIntro
sortSorts the elements in the range into ascending order. Equivalent elements are not guaranteed to keep their original relative order.
void sort (RandomAccessIterator first, RandomAccessIterator last);
void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);
comp is a binary function that accepts two elements in the range as arguments, and returns a value convertible to bool. The value returned indicates whether the element passed as first argument is considered to go before the second in the specific strict weak ordering it defines.
e.g. descending order [](T n1, T n2){return n1 > n2;}
stable_sortpreserves the relative order of the elements with equivalent values.
partial_sortunfiishing sort
void partial_sort (RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, Compare comp);
is_sortedbool is_sorted (ForwardIterator first, ForwardIterator last, Compare comp);
is_sorted_untilFind first unsorted element
ForwardIterator is_sorted_until (ForwardIterator first, ForwardIterator last, Compare comp);

VI. Binary search

VII. Merge

VIII. Heap

IX. Min/max

X.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值