Equal

  • 两个序列在[first,last)区间内相等,equal()返回true。以第一个序列作为基准,如果第二序列元素多不予考虑,如果要保证两个序列完全相等需要比较元素的个数
if(vec1.size() == vec2.size() && equal(vec1.begin(),vec1.end(),vec2.begin()));
  • 或者可以使用容器提供的equality操作符,例如 vec1 == vec2
  • 如果第二序列比第一序列的元素少 ,算法内部进行迭代行为的时候,会超越序列的尾端,造成不可预测的结果
  • 第一版本缺省使用元素型别所提供的equality 操作符来进行大小的比较;第二版本使用指定的仿函数pred作为比较的依据

template <class InputIterator1,class InputIterator2>
inline bool equal(InputIterator1 first1,InputIterator1 last1,InputIterator2 first2){//将序列一走过一遍,序列二亦步亦趋//如果序列一的元素多过序列二的元素个数 就会糟糕了while(first1 != first2){if(!(*first1 == *first2)){return false;}++first1;++first2;}//第二个版本
//    for (; first1 != last1;++first1,++first2){
//        if(*first1 != *first2){
//            return false;
//        }
//    }return true;
}//使用自定义的 仿函数pred作为比较的依据
template <class InputIterator1,class InputIterator2,class BinaryOperation>
inline bool equal(InputIterator1 first1,InputIterator1 last1,InputIterator2 first2,BinaryOperation binary_pred){while(first1 != first2){if(!binary_pred(*first1,*first2)){return false;}++first1;++first2;}return true;
}
#include <iostream>   //std::cout
#include <algorithm>  //std::equal
#include <vector>     //std::vectorbool my_predicate(int i,int j){return (i==j);
}int main(){int myints[] = {20,40,60,80,100};std::vector<int>my_vector(myints,myints+5);//using default comparison:if(std::equal(my_vector.begin(),my_vector.end(),myints)){std::cout << " equal! "<< std::endl;}else{std::cout << " differ! " << std::endl;}my_vector[3] = 94;if(std::equal(my_vector.begin(),my_vector.end(),myints, my_predicate)){std::cout << " equal! "<< std::endl;}else{std::cout << " differ! " << std::endl;}return 0;
}

fill

  • 将指定[first,last)内的所有元素使用新值填充
template<class ForwardIterator,class T>
void fill(ForwardIterator first,ForwardIterator last,const T& value){while (first!=last){*first = value;   //设定新值++first;          //迭代走过整个区间}
}
#include <iostream>   //std::cout
#include <algorithm>  //std::fill
#include <vector>     //std::vectorbool my_predicate(int i,int j){return (i==j);
}int main(){std::vector<int>my_vector(8); //0 0 0 0 0 0 0 0std::fill(my_vector.begin(),my_vector.begin()+4,5);std::fill(my_vector.begin()+3,my_vector.end()-2,8);std::cout << "my_vector contains:"<< std::endl;for(std::vector<int>::iterator it = my_vector.begin();it != my_vector.end();++it){std::cout << *it << " ";}return 0;
}

fill_n

  • 将本地[first,last)内的前n个元素改写为新的数值,返回迭代器指向被填入的最后一个元素的下一个位置
template<class OutputIterator,class Size,class T>
OutputIterator fill_n(OutputIterator first,Size n,const T&value){while (n>0){*first = value;--n;++first;}return first;
}
  • 如果n超过了容器的容量的上限,会造成什么样的后果呢?
    int ia[3] = {0,1,2};std::vector<int>iv(ia,ia+3);std::fill_n(iv.begin(),5,7);
  • 迭代器进行的是assignment操作是一种覆写操作,一旦超越了容器的大小会造成不可预期的结果。
  • 解决方法:使用insert产生一个具有插入性质而不是覆写能力的迭代器。inserter()可以产生一个用来修饰迭代器的配接器,用法如下
    int ia[3] = {0,1,2};std::vector<int>iv(ia,ia+3);
//    std::fill_n(iv.begin(),5,7);   //7 7 7std::fill_n(std::inserter(iv,iv.begin()),5,7); // 7 7 7 7 7 0 1 2 

iter_swap

template <class ForwardIterator1, class ForwardIterator2>void iter_swap (ForwardIterator1 a, ForwardIterator2 b)
{swap (*a, *b);
}
// iter_swap example
#include <iostream>     // std::cout
#include <algorithm>    // std::iter_swap
#include <vector>       // std::vectorint main () {int myints[]={10,20,30,40,50 };              //   myints:  10  20  30  40  50std::vector<int> myvector (4,99);            // myvector:  99  99  99  99std::iter_swap(myints,myvector.begin());     //   myints: [99] 20  30  40  50// myvector: [10] 99  99  99std::iter_swap(myints+3,myvector.begin()+2); //   myints:  99  20  30 [99] 50// myvector:  10  99 [40] 99std::cout << "myvector contains:";for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)std::cout << ' ' << *it;std::cout << '\n';return 0;
}

lexicographical_compare

  • 操作指针比较[first1 , last1) 和 [first2 , last2)对应位置上的元素大小的比较,持续到(1)某一组元素数据不相等;(2)二者完全相等,同时到达两个队列的末尾(3)到达last1或者last2
  • 要求第一序列按照字典排序方式而言不小于第二序列

  • lexicographical_compare - C++ Reference
template<class InputIterator1,class InputIterator2>
bool lexicographical_compare(InputIterator1 first1,InputIterator1 last1,InputIterator2 first2,InputIterator2 last2){while (first1!=last1){if (first2 == last2 || *first2 < *first1){return false;} else if(*first1 < *first2){return true;}++first1;++first2;}return (first2 != last2);
}
// lexicographical_compare example
#include <iostream>     // std::cout, std::boolalpha
#include <algorithm>    // std::lexicographical_compare
#include <cctype>       // std::tolower// a case-insensitive comparison function:
bool mycomp (char c1, char c2)
{ return std::tolower(c1)<std::tolower(c2); }int main () {char foo[]="Apple";char bar[]="apartment";std::cout << std::boolalpha;std::cout << "Comparing foo and bar lexicographically (foo<bar):\n";std::cout << "Using default comparison (operator<): ";std::cout << std::lexicographical_compare(foo,foo+5,bar,bar+9);std::cout << '\n';std::cout << "Using mycomp as comparison object: ";std::cout << std::lexicographical_compare(foo,foo+5,bar,bar+9,mycomp);std::cout << '\n';return 0;
}

max

  • 取两个对象中的最大数值
  • 版本一使用greater-than操作符号进行大小的比较,版本二使用自定义comp函数比较
template <class T>
const T&max(const T&a,const T&b){return (a>b)?a:b;
}template <class T,class BinaryOperation>
const T&max(const T&a,const T&b,BinaryOperation binary_pred){return (binary_pred(a,b))?a:b;
}

min

  • 取两个对象中的最小数值
  • 版本一使用less-than操作符号进行大小的比较,版本二使用自定义comp函数比较
template <class T>
const T&min(const T&a,const T&b){return (a<b)?a:b;
}template <class T,class BinaryOperation>
const T&min(const T&a,const T&b,BinaryOperation binary_pred){return (binary_pred(a,b))?a:b;
}

mismatch

  • 判断两个区间的第一个不匹配的点,返回一个由两个迭代器组成的pair;pair的第一个迭代器指向的是第一区间第一个不匹配的点,第二个迭代器指向第二个区间的不匹配的点
  • 需要首先检查迭代器是否不等于容器的end()
  • 如果两个序列的所有元素对应都匹配,返回的是两个序列各自的last迭代器,缺省情况下使用equality操作符号来比较元素;第二版本允许用户指定自己的比较操作
  • 需要第二个序列的元素个数要大于第一个序列,否则会发生不可预期的行为

template <class InputIterator1,class InputIterator2>
std::pair<InputIterator1,InputIterator2>mismatch(InputIterator1 first1,InputIterator1 last1,InputIterator2 first2){while ((first1!=last1)&&(*first1 == *first2)){++first1;++first2;}return std::make_pair(first1,first2);
}
  • 第二版本 pred(*first1,*first2)

swap

  • 对调元素的内容
template <class T>
inline void swap(T&a,T&b){T tmp (a);a = b;b = tmp;
}

STL源码剖析 基本算法 equal | fill | iter_awap | lexicographical_compare | max | min | swap |mismatch相关推荐

  1. STL源码剖析 基本算法 < stl_algobase.h >

    注意事项 : 实际使用的时候,使用的是<algorithm>这个头文件,不是题目中的< stl_algobase.h > equal函数 如果两个序列在[firsLlast) ...

  2. STL源码剖析之算法:lower_bound

    lower_bound()函数返回能够插入value的第一个位置,该函数要求目标序列必须是有序的. template <class ForwardIterator, class T> in ...

  3. STL源码剖析 数值算法 copy 算法

    copy复制操作,其操作通过使用assignment operator .针对使用trivial assignment operator的元素型别可以直接使用内存直接复制行为(使用C函数 memove ...

  4. STL源码剖析 算法开篇

    STL源码剖析 算法章节 算法总览_CHYabc123456hh的博客-CSDN博客 质变算法 质变算法 - 会改变操作对象的数值,比如互换.替换.填写.删除.排列组合.分隔.随机重排.排序等 #in ...

  5. 【STL源码剖析】list模拟实现 | 适配器实现反向迭代器【超详细的底层算法解释】

    今天博主继续带来STL源码剖析专栏的第三篇博客了! 今天带来list的模拟实现! 话不多说,直接进入我们今天的内容! 前言 那么这里博主先安利一下一些干货满满的专栏啦! 手撕数据结构https://b ...

  6. STL源码剖析——P142关于list::sort函数

    在list容器中,由于容器自身组织数据的特殊性,所以list提供了自己的排序函数list::sort, 并且实现得相当巧妙,不过<STL源码剖析>的原文中,我有些许疑问,对于该排序算法,侯 ...

  7. STL源码剖析学习二:空间配置器(allocator)

    STL源码剖析学习二:空间配置器(allocator) 标准接口: vlaue_type pointer const_pointer reference const_reference size_ty ...

  8. C++ STL源码剖析 笔记

    写在前面 记录一下<C++ STL源码剖析>中的要点. 一.STL六大组件 容器(container): 各种数据结构,用于存放数据: class template 类泛型: 如vecto ...

  9. 【《STL源码剖析》提炼总结】 第1节:空间配置器 allocator

    文章目录 一. 什么是空间配置器 二. STL allocator的四个操作: allocate,deallocate,construct,destroy `construct()` `destroy ...

最新文章

  1. ip设置 kali 重置_在 Windows 系统中如何重置 TCP/IP 协议堆栈修复网络连接问题
  2. linux 协议栈 位置,[置顶] Linux协议栈代码阅读笔记(一)
  3. [iPhone高级] 基于XMPP的IOS聊天客户端程序(IOS端三)
  4. 状态码301 302
  5. 微信小程序开发学习笔记006--微信小程序组件详解02
  6. C# DataGridView 如何选中整行
  7. 谁说 Java 要过时?2017 年 Java 大事件回顾!
  8. 第六章 输入输出系统-作业
  9. 一文读懂机器学习、数据科学、人工智能、深度学习和统计学之间的区别
  10. 从一个小场景学会使用 apply方法
  11. 软件项目管理实用教程(人民邮电出版)第二章课后习题
  12. python爬虫实例(百度图片、网站图片)
  13. 爬虫网页框架代码和媒体对象
  14. 唐巧的《iOS开发进阶》 - 读后感
  15. 下载open jdk 和阿里Alibaba Dragonwell (开源open JDK)
  16. 除了编码,还要会说话(1)
  17. 4 计算机设备的折旧年限不低于,汇算清缴十大注意事项四:如何正确适用固定资产加速折旧政策...
  18. 怎么样删除计算机管理员用户账户,怎么样删除电脑中多出来的管理员账户
  19. MySQL三 插入语句包含查询语句
  20. 如何利用互联网思维,让用户从“被动选择”到“主动选择”?

热门文章

  1. ArcGIS如何在一个矢量上用不同颜色进行标注
  2. 【转】[完全免费] 在线UML Sequence Diagram 时序图工具 - 教程第3部分
  3. 第八节: EF的性能篇(一) 之 EF自有方法的性能测试
  4. linux下java程序实现重启功能
  5. 【BZOJ - 3993】星际战争(网络流最大流+二分)
  6. 【牛客 - 317D】小a与黄金街道(数论,tricks)
  7. 算法讲解 -- 区间dp经典模型与优化(石子归并)
  8. 【CodeForces - 195D】Analyzing Polyline (思维,卡精度的处理方式)
  9. 【HDU - 2112】 HDU Today(dijkstra单源最短路 + map转换)
  10. c语言中只能逐个引用6,C语言前面六个练习.doc