euqal

  比较两个序列是否相等,相等返回true,不相等返回false

  1. template <class _InputIter1, class _InputIter2>//版本1
  2. inline bool equal(_InputIter1 __first1, _InputIter1 __last1,
  3. _InputIter2 __first2) {
  4. for ( ; __first1 != __last1; ++__first1, ++__first2)//遍历区间[first,last)元素
  5. if (*__first1 != *__first2)//只有有一个不相等返回false
  6. return false;
  7. return true;
  8. }
  9.  
  10. template <class _InputIter1, class _InputIter2, class _BinaryPredicate>//版本2
  11. inline bool equal(_InputIter1 __first1, _InputIter1 __last1,
  12. _InputIter2 __first2, _BinaryPredicate __binary_pred) {
  13. for ( ; __first1 != __last1; ++__first1, ++__first2)
  14. if (!__binary_pred(*__first1, *__first2))//两个元素执行二元操作符
  15. return false;
  16. return true;
  17. }

fill

  将value值填充整个区间,不能为OutputIterator,因为fill会用到first和last,outputIterator无法做相等的测试

  1. template <class ForwardIterator, class T>
  2. void fill( ForwardIterator first, ForwardIterator last,const T& value);

fill_n

  会将数值value填充[first,first+n),返回值为first+n,可以用outputIterator

  1. template <class OutputIterator,class size,class T>
  2. OutputIterator fill_n( OutputIterator first, OutputIterator last,size n,const T& value);

iter_swap

  将两个迭代器所指对象互换

  1. template <class _ForwardIter1, class _ForwardIter2, class _Tp>
  2. inline void __iter_swap(_ForwardIter1 __a, _ForwardIter2 __b, _Tp*) {
  3. _Tp __tmp = *__a;
  4. *__a = *__b;
  5. *__b = __tmp;
  6. }
  7.  
  8. template <class _ForwardIter1, class _ForwardIter2>
  9. inline void iter_swap(_ForwardIter1 __a, _ForwardIter2 __b) {
  10. __iter_swap(__a, __b, __VALUE_TYPE(__a));
  11. }

generate

  1. template <class ForwardIterator, class Generator>
  2. void generate ( ForwardIterator first, ForwardIterator last, Generator gen )
  3. {
  4. while (first != last)
  5. {
  6. *first = gen();
  7. ++first;
  8. }
  9. }

generate_n

  返回值是first+n,generate和generate_n中的gen会被调用n(或first+n)次,而非只在循环外调用一次,这一点很重要,因为Generate不一定会在每次调用时候都返回相同的结果,因此generate允许从文件读入,取局部状态的值并更改。

  1. template <class OutputIterator, class Size, class Generator>
  2. OutputIterator generate_n ( OutputIterator first, Size n,const Generator gen )
  3. {
  4. while (n>)
  5. {
  6. *first = gen();
  7. ++first;
  8. --n;
  9. }
  10. }

mismatch

  与equal类似,但会返回哪里不同,equal(f1,l1,f2)与misatch(f1,l1,f2)。first==l1等价,如果相等,返回两个序列的last

  1. //版本一:调用重载operator==比较元素
  2. template <class InputIterator1,class InputIterator2>
  3. pair<InputIterator1,InputIterator2> mismatch(InputIterator1 first1,InputIterator1 last1,InputIterator2 first2)
  4. {
  5. while (first1 != last1 && *first1 == *first2) {
  6. ++first1;
  7. ++first2;
  8. }
  9. return pair<InputIterator1,InputIterator2>(first1, first2);//返回pair类型,first1指向第一个序列不匹配点,first2指向第二个序列不匹配点
  10. }
  11. //版本二:调用自己定义的function object来比较
  12. template <class InputIterator1,class InputIterator2,class BinaryPredicate>
  13. pair<InputIterator1,InputIterator2> mismatch(InputIterator1 first1,InputIterator1 last1,InputIterator2 first2,BinaryPredicate binary_pred)
  14. {
  15. while (first1 != last1 && binary_pred(*first1, *first2)) {
  16. ++first1;
  17. ++first2;
  18. return pair<InputIterator1,InputIterator2>(first1, first2);
  19. }

lexicographical_compare

  返回两个序列的字典排序大小,

  1. 如果第一序列元素较小返回true,否则返回false
  2. 到达last1而未到达last2返回true
  3. 到达last2而未到达last1返回false
  4. 同时到达last1和last2(所有元素都匹配)返回false
  1. //版本一:调用重载operator==比较元素
  2. template <class InputIterator1,class InputIterator2>
  3. bool lexicographical_compare(InputIterator1 first1,InputIterator1 last1,InputIterator2 first2,InputIterator2 last2)
  4. {
  5. for ( ; first1 != last1 && first2 != last2; ++first1, ++first2)//两组元素一一比较,除非长度不相同
  6. {
  7. if (*first1 < *first2)
  8. return true;
  9. if (*first2 < *first1)
  10. return false;
  11. }
  12. return first1 == last1 && first2 != last2;//第二组还有元素
  13. }
  14.  
  15. //版本二:调用自己定义的function object来比较,inputiterator1 value_type可转化为 BinaryPredicate的第一型别等等
  16. template <class InputIterator1,class InputIterator2,class BinaryPredicate>
  17. bool lexicographical_compare(InputIterator1 first1,InputIterator1 last1,InputIterator2 first2,InputIterator2,BinaryPredicate binary_pred)
  18. {
  19. for ( ; first1 != last1 && first2 != last2; ++first1, ++first2)
  20. {
  21. if (comp(*first1, *first2))
  22. return true;
  23. if (comp(*first2, *first1))
  24. return false;
  25. }
  26. return first1 == last1 && first2 != last2;
  27. }

copy

  1. 为outputIterator中的元素赋值而不是产生新的元素,所以outputIterator不能是空的
  2. 如要元素安插序列,使用insert成员函数或使用copy搭配insert_iterator适配器
  3. 如果输出区间的起头与输入区间重叠,则不能使用copy,如果输出区间的尾端与输入区间重叠,则可以使用;copy_backward的限制与copy相反

  与strcpy、memmove的情况类似,具体看下图

  在偏特化与全特化中分析过, 最适合的函数会优先调用, 普通函数优先级大于模板函数

  1. template <class InputIterator, class OutputIterator>
  2. inline OutputIterator copy(InputIterator first, InputIterator last, OutputIterator result)
  3. {
  4. return __copy_dispatch<InputIterator,OutputIterator>()(first, last, result);
  5. }
  6. // 重载 在偏特化与全特化中分析过, 最适合的函数会优先调用, 普通函数优于模板函数
  7. inline char* copy(const char* first, const char* last, char* result) {
  8. // 直接调用memmove效率最高
  9. memmove(result, first, last - first);
  10. return result + (last - first);
  11. }
  12. inline wchar_t* copy(const wchar_t* first, const wchar_t* last, wchar_t* result) {
  13. // 直接调用memmove效率最高
  14. memmove(result, first, sizeof(wchar_t) * (last - first));
  15. return result + (last - first);
  16. }
  17.  
  18. template <class InputIterator, class OutputIterator>
  19. struct __copy_dispatch//通过传入参数的迭代器类型再进行优化处理
  20. {
  21. OutputIterator operator()(InputIterator first, InputIterator last,
  22. OutputIterator result) {
  23. // iterator_category获取迭代器类型, 不同迭代器选择不同的重载函数
  24. return __copy(first, last, result, iterator_category(first));
  25. }
  26. };
  27. template <class T>
  28. struct __copy_dispatch<const T*, T//针对参数是否是原生指针进行泛化
  29. {
  30. T* operator()(const T* first, const T* last, T* result) {
  31. typedef typename __type_traits<T>::has_trivial_assignment_operator t;
  32. return __copy_t(first, last, result, t());
  33. }
  34. };
  35.  
  36. template <class T>
  37. struct __copy_dispatch<T*, T*>
  38. {
  39. T* operator()(T* first, T* last, T* result) {
  40. typedef typename __type_traits<T>::has_trivial_assignment_operator t;
  41. return __copy_t(first, last, result, t());
  42. }
  43. };
  44.  
  45. template <class InputIterator, class OutputIterator>
  46. inline OutputIterator __copy(InputIterator first, InputIterator last,OutputIterator result, input_iterator_tag)
  47. {
  48. // 通过迭代器将一个元素一个元素的复制
  49. for ( ; first != last; ++result, ++first)
  50. *result = *first;
  51. return result;
  52. }
  53. template <class RandomAccessIterator, class OutputIterator>
  54. inline OutputIterator __copy(RandomAccessIterator first, RandomAccessIterator last, OutputIterator result, random_access_iterator_tag)
  55. {
  56. return __copy_d(first, last, result, distance_type(first));
  57. }
  58. template <class RandomAccessIterator, class OutputIterator, class Distance>
  59. //优化处理
  60. template <class T>
  61. inline T* __copy_t(const T* first, const T* last, T* result, __false_type) {
  62. return __copy_d(first, last, result, (ptrdiff_t*) );
  63. }
  64. //优化处理
  65. template <class T>
  66. inline T* __copy_t(const T* first, const T* last, T* result, __true_type) {
  67. memmove(result, first, sizeof(T) * (last - first));
  68. return result + (last - first);
  69. }
  70. inline OutputIterator __copy_d(RandomAccessIterator first, RandomAccessIterator last,OutputIterator result, Distance*)
  71. {
  72. // 通过迭代器之间的元素个数将一个元素一个元素的复制
  73. for (Distance n = last - first; n > ; --n, ++result, ++first)
  74. *result = *first;
  75. return result;
  76. }

copy_backward

  从结果集的尾端到头端复制元素,返回result-(last-first)

  1. template <class BidirectionalIterator1,class BidirectionalIterator2>
  2. OutputeIterator copy(BidirectionalIterator1 first,BidirectionalIterator1 last,BidirectionalIterator2 result);

比较两个ranges(equal,mismatch,lexicographical_compare)的更多相关文章

  1. 不要重复发明轮子-C++STL

    闫常友 著. 中国铁道出版社. 2013/5 标准的C++模板库,是算法和其他一些标准组件的集合. . 1.类模板简介 2.c++中的字符串 3.容器 4.c++中的算法 5.迭代器 6.STL 数值 ...

  2. STL基础--算法(不修改数据的算法)

    不修改数据的算法 count, min and max, compare, linear search, attribute // 算法中Lambda函数很常用: num = count_if(vec ...

  3. 机器学习基础与实践(三)----数据降维之PCA

    写在前面:本来这篇应该是上周四更新,但是上周四写了一篇深度学习的反向传播法的过程,就推迟更新了.本来想参考PRML来写,但是发现里面涉及到比较多的数学知识,写出来可能不好理解,我决定还是用最通俗的方法 ...

  4. pandas numpy处理缺失值,none与nan比较

    原文链接:https://junjiecai.github.io/posts/2016/Oct/20/none_vs_nan/ 建议从这里下载这篇文章对应的.ipynb文件和相关资源.这样你就能在Ju ...

  5. T检验与F检验的区别_f检验和t检验的关系

    1,T检验和F检验的由来 一般而言,为了确定从样本(sample)统计结果推论至总体时所犯错的概率,我们会利用统计学家所开发的一些统计方法,进行统计检定. 通过把所得到的统计检定值,与统计学家建立了一 ...

  6. 转:最近5年133个Java面试问题列表

    最近5年133个Java面试问题列表 Java 面试随着时间的改变而改变.在过去的日子里,当你知道 String 和 StringBuilder 的区别就能让你直接进入第二轮面试,但是现在问题变得越来 ...

  7. linux test 命令使用

    1. 关于某个文件名的『类型』侦测(存在与否),如 test -e filename -e 该『文件名』是否存在?(常用) -f 该『文件名』是否为文件(file)?(常用) -d 该『文件名』是否为 ...

  8. equals()和hashCode()隐式调用时的约定

    package com.hash; import java.util.HashMap; public class Apple { private String color; public Apple( ...

  9. 通俗理解T检验和F检验

    来源: http://blog.sina.com.cn/s/blog_4ee13c2c01016div.html   1,T检验和F检验的由来 一般而言,为了确定从样本(sample)统计结果推论至总 ...

随机推荐

  1. DevExpress WinForms v18.2新版亮点(六)

    行业领先的.NET界面控件2018年第二次重大更新——DevExpress v18.2日前正式发布,本站将以连载的形式为大家介绍各版本新增内容.本文将介绍了DevExpress WinForms v1 ...

  2. Gym - 100989M(dp)

    George met AbdelKader in the corridor of the CS department busy trying to fix a group of incorrect e ...

  3. Vuejs2.0学习(Render函数,createElement,vm.$slots)

    直接来到进阶部分, Render函数 直接来到Render,本来也想跳过,发现后面的路由貌似跟它还有点关联.先来看看Render 1.1 官网一开始就看的挺懵的,不知道讲的是啥,动手试了一下,一开头讲 ...

  4. Linux 安装最新版本python3

    新安装了Linux系统(CentOS 6),发现已安装的python版本是2.6. 在网上搜索研究之后总结了一下怎么在保留python2的同时安装最新版的python3. 1. 查看 Python 的 ...

  5. Delphi 10.3.1来了

    10.3.1发布了,这个版本可以独自安装,是对Delphi 10.3 Rio,C ++ Builder 10.3 Rio和RAD Studio 10.3 Rio的更新.如果安装了2018年11月发布的 ...

  6. Python中列表、元组、集合、字典

    Python 列表(List) 列表是最常用的Python数据类型: 列表中的数据项不需要具有相同的类型: 列表也叫做数组,定义时使用[]: 通过下标访问列表中的元素,下标从 0  开始计数 列表的增 ...

  7. mysql增删改查练习

    Mysql增删改查sql语句练习 关于数据库的一些操作: 进入mysql 命令行: mysql -uroot –p 查看所有数据库: show databases; 创建数据库: create dat ...

  8. 识别图片验证码的三种方式(scrapy模拟登陆豆瓣网)

    1.通过肉眼识别,然后输入到input里面 from PIL import image Image request.urlretrieve(url,'image')  #下载验证码图片 image = ...

  9. deconvolution layer parameter setting

    reference: 1. Paper describes initializing the deconv layer with bilinear filter coefficients and tr ...

  10. SpringBoot 配置文件详解

    springboot采纳了建立生产就绪spring应用程序的观点. Spring Boot优先于配置的惯例,旨在让您尽快启动和运行.在一般情况下,我们不需要做太多的配置就能够让spring boot正 ...