《c++ primer》学习笔记——泛型算法01
来源:互联网 发布:linux oracle创建用户 编辑:程序博客网 时间:2024/05/17 05:07
头文件:
#include <algorithm>
#include <numeric>
1、accumulate
2、find_first_of
3、fill
4、fill_n
5、copy
6、replace
7、replace_copy
8、sort
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.
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.
11、count_if
12、find_if
#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.
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; }
- 《c++ primer》学习笔记——泛型算法01
- C++Primer笔记——泛型算法
- 【C++primer学习笔记】第11章 泛型算法
- C++Primer学习笔记(11)泛型算法
- C++ Primer学习笔记——$11 泛型算法
- 《c++ primer》学习笔记——泛型算法
- C++primer阅读笔记---------泛型算法
- C++primer学习:泛型算法(1)
- c++primer学习:泛型算法(2)
- C++primer学习:泛型算法(3)
- c++primer学习笔记 - 容器和算法
- C++Primer学习笔记第十一章(11/18)泛型算法
- 2016/12/08C++Primer学习笔记:第十章 泛型算法
- 《C++primer(第五版)》学习笔记-第十章:泛型算法
- c++primer阅读笔记之泛型算法
- C++PRIMER学习笔记
- c++primer学习笔记
- c++primer学习笔记
- BroadcastReceiver
- 为什么java.net.URLEncoder.encode方法要调用两次而不是一次呢?
- C++学习路线
- 主键与索引,主键与外键的关系
- 如何在恋爱中识别Loser?
- 《c++ primer》学习笔记——泛型算法01
- Object Recognition - Bag of Keypoints
- C 陷阱与缺陷 笔记
- android实现自动log在sd卡上
- 分析网站的整理情况(排名、点击率、转化率等等)
- QT编译./configure参数的详细解释
- Qt入门笔记----Windows版
- QT基础学习笔记
- Shell 学习14(tr命令)