《c++ primer》学习笔记——泛型算法01

来源:互联网 发布:linux oracle创建用户 编辑:程序博客网 时间:2024/05/17 05:07
头文件:
#include <algorithm>
#include <numeric>


算法:

0、back_inserter

template <class Container>  back_insert_iterator<Container> back_inserter (Container& x);

Construct a back insert iterator
This function generates a back insert iterator for a container.

1、accumulate
template <class InputIterator, class T>   T accumulate ( InputIterator first, InputIterator last, T init ){  while ( first!=last )    init = init + *first++;  // or: init=binary_op(init,*first++) for the binary_op version  return init;}

2、find_first_of
template<class ForwardIterator1, class ForwardIterator2>  ForwardIterator1 find_first_of ( ForwardIterator1 first1, ForwardIterator1 last1,                                   ForwardIterator2 first2, ForwardIterator2 last2){  for ( ; first1 != last1; ++first1 )    for (ForwardIterator2 it=first2; it!=last2; ++it)      if (*it==*first1)          // or: if (comp(*it,*first)) for the pred version        return first1;  return last1;}

3、fill
template < class ForwardIterator, class T >  void fill ( ForwardIterator first, ForwardIterator last, const T& value ){  while (first != last)  *first++ = value;}

4、fill_n
template < class OutputIterator, class Size, class T >  void fill_n ( OutputIterator first, Size n, const T& value ){  for (; n>0; --n)  *first++ = value;}

5、copy
template<class InputIterator, class OutputIterator>  OutputIterator copy ( InputIterator first, InputIterator last, OutputIterator result ){  while (first!=last) *result++ = *first++;  return result;}

6、replace
template < class ForwardIterator, class T >  void replace ( ForwardIterator first, ForwardIterator last,                 const T& old_value, const T& new_value ){  for (; first != last; ++first)    if (*first == old_value) *first=new_value;}

7、replace_copy
template < class InputIterator, class OutputIterator, class T >  OutputIterator replace_copy ( InputIterator first, InputIterator last,                                OutputIterator result, const T& old_value, const T& new_value ){  for (; first != last; ++first, ++result)    *result = (*first==old_value)? new_value: *first;  return result;}

8、sort
template <class RandomAccessIterator>  void sort ( RandomAccessIterator first, RandomAccessIterator last );template <class RandomAccessIterator, class Compare>  void sort ( RandomAccessIterator first, RandomAccessIterator last, Compare comp );
Sort elements in range
Sorts the elements in the range [first,last) into ascending order.The elements are compared using operator< for the first version, and comp for the second.Elements that would compare equal to each other are not guaranteed to keep their original relative order.


9、stable_sort
template <class RandomAccessIterator>  void stable_sort ( RandomAccessIterator first, RandomAccessIterator last );template <class RandomAccessIterator, class Compare>  void stable_sort ( RandomAccessIterator first, RandomAccessIterator last,                     Compare comp );
Sort elements preserving order of equivalents
Sorts the elements in the range [first,last) into ascending order, like sort, but stable_sort grants that the relative order of the elements with equivalent values is preserved.
The elements are compared using operator< for the first version, and comp for the second.


10、unique

template <class ForwardIterator>  ForwardIterator unique ( ForwardIterator first, ForwardIterator last );template <class ForwardIterator, class BinaryPredicate>  ForwardIterator unique ( ForwardIterator first, ForwardIterator last,                           BinaryPredicate pred );template <class ForwardIterator>  ForwardIterator unique ( ForwardIterator first, ForwardIterator last ){  ForwardIterator result=first;  while (++first != last)  {    if (!(*result == *first))  // or: if (!pred(*result,*first)) for the pred version      *(++result)=*first;  }  return ++result;}

11、count_if
template <class InputIterator, class Predicate>  ptrdiff_t count_if ( InputIterator first, InputIterator last, Predicate pred ){  ptrdiff_t ret=0;  while (first != last) if (pred(*first++)) ++ret;  return ret;}

12、find_if
template<class InputIterator, class Predicate>  InputIterator find_if ( InputIterator first, InputIterator last, Predicate pred )  {    for ( ; first!=last ; first++ ) if ( pred(*first) ) break;    return first;  }


原创粉丝点击