C++、STL标准模板库和泛型编程 ——迭代器、 算法、仿函数(侯捷)

这篇具有很好参考价值的文章主要介绍了C++、STL标准模板库和泛型编程 ——迭代器、 算法、仿函数(侯捷)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

侯捷 C++八部曲笔记汇总 - - - 持续更新 ! ! !
一、C++ 面向对象高级开发
1、C++面向对象高级编程(上)
2、C++面向对象高级编程(下)
二、STL 标准库和泛型编程
1、分配器、序列式容器
2、关联式容器
3、迭代器、 算法、仿函数
4、适配器、补充
三、C++ 设计模式
四、C++ 新标准
五、C++ 内存管理机制
六、C++ 程序的生前和死后

使用一个东西,却不明白它的道理,不高明!—— 林语堂

阶段学习
使用C++标准库
认识C++标准库(胸中自有丘壑!)
良好使用C++标准库
扩充C++标准库

所谓 Generic ProgrammingGP,泛型编程),就是使用 template (模板)为主要工具来编写程序。

  • GP 是将 datasmethods 分开来;

    • ContainersAlgorithms可各自闭门造车﹐其间以Iterator连通即可·
    • Algorithms通过Iterators确定操作范围﹐也通过Iterators取用 Container元素。
  • OOP(Object-Oriented Programming),企图将 datasmethods 关联在一起。

C++标准模板库Standard Template 最重要的六大部件(Components):容器算法仿函数迭代器适配器分配器

  • 容器(Containers)是class template
  • 算法(Algorithms)是function template其内最终涉及元素本身的操作,无非就是比大小!)
  • 迭代器(Iterators)是class template
  • 仿函数(Functors)是class template
  • 适配器(Adapters)是class template
  • 分配器(Allocators)是class template

关系图:
C++、STL标准模板库和泛型编程 ——迭代器、 算法、仿函数(侯捷)

迭代器

前面说到迭代器中必须要有5种关联类型:pointerreferenceiterator_category(迭代器类型,比如说随机存取)、value_type(值存放类型)、difference_type(容器两个元素之间的距离类型)。

iterator_category

也有五种迭代器类型:随机存取迭代器arrayvectordeque)、双向迭代器list红黑树容器)、单向迭代器forward_listhash类容器)、输入迭代器istream迭代器),输出迭代器ostream迭代器)。

C++、STL标准模板库和泛型编程 ——迭代器、 算法、仿函数(侯捷)
既然是tag,为什么要用有继承关系的class来实现呢?

  • 方便迭代器类型作为参数进行传递,如果是整数的是不方便的;还有一个原因,有些算法的实现没有实现特定类型的迭代器类别,就可以用继承关系去找父迭代器类别。

C++、STL标准模板库和泛型编程 ——迭代器、 算法、仿函数(侯捷)
iterator_category对算法的影响

以这个distance函数为例,会根据迭代器的类别来调用不同的具体实现函数,

  • 一个是只包含一个减法操作的语句,
  • 一个是包含一个while循环的语句,可想而知,当真实距离很大时,有while循环的具体实现函数效率会非常低下。

使用萃取机iterator_traits,虽然只有两种,但是这几种类型都是继承关系is a 父类input_iterator_tag,如果没有重载,最终都会落到input_iterator_tag

C++、STL标准模板库和泛型编程 ——迭代器、 算法、仿函数(侯捷)

看一个特别能体现C++注重效率的体现:
copy实现,到最终的实现细节,经过了很多判断,这些判断是为了找到最高效率的实现,就是判断迭代器的分类。

C++、STL标准模板库和泛型编程 ——迭代器、 算法、仿函数(侯捷)
C++、STL标准模板库和泛型编程 ——迭代器、 算法、仿函数(侯捷)

算法

Algorithms看不见Containers,对其一无所知;所以,它所需要的一切信息都必须从Iterators取得,而Iterators(由Containers供应)必须能够回答Algorithm的所有提问,才能搭配该Algorithm的所有操作。

template<typename Iterator>
Algorithm(Iterator it1, Iterator it2){
	...
}

template<typename Iterator, typename Cmp>//Cmp为传入的一个准则,比如比大小准则
Algorithm(Iterator it1, Iterator it2, Cmp comp){
	...
}

accumulate

template<class InputInterator, class T>
T accumulate(InputIterator first, InputIterator last, T init){...}

//另外一个版本为:
template<class InputInterator, class T, class BinaryOperation>
T accumulate(InputIterator first, InputIterator last, T init, BinaryOperation binary_op){...}

上面这个binary_op指明是一个二元操作数的函数,可以是仿函数(实质上是一个类),也可以是函数,只要是能够在该算法的函数体内通过小括号调用就行!!!!

  • 也就是能够这么用:binary_op(a, b);
  • 就算是在算法(函数)里面,也能够使用仿函数,但是传入的是仿函数的对象实例,而如果要传入函数的话,就传函数名就可以了。
int init = 100;
int nums[] = {10,20,30};
accumulate(nums, nums+3, init);//不指定具体怎么操作,默认为加法,输出160
accumulate(nums, nums+3, init, minus<int>()); //这minus时减法的意思,所以输出为40

for_each

  • 对容器区间内的元素做同样的事情。
template<class InputIterator, class Function>
Function for_each(InputIterator first, InputIterator last, Function f){...}

replace

1、 replace

  • 将容器区间内的元素进行替换,如果元素值等于old_value就把它替换为new_value.
template<class ForwardIterator, class T>
void replace(ForwardIterator first, ForwardIterator last, const T& old_value, const T& new_value){...}

2、replace_if

  • 在算法中看到 if ,就代表你要给他一个条件。
  • Predicate为一个条件,判断式子(传回),如果符合条件就进行替换
template<class ForwardIterator, class Predicate, class T>
void replace_copy(ForwardIterator first, ForwardIterator last,  Predicate pred, const T& new_value){...}

3、replace_copy

  • 范围内所有等同于old_value的都以new_value放置新的区间中,不符合原值的也放入新的区间
template<class InputIterator, class OutputIterator, class T>
OutputIterator replace_copy(InputIterator first, InputIterator last, OutputIterator result, const T& old_value, const T& new_value)
{...}

count

容器不带成员函数count():

array, vector, list, forward_list, deque

容器带有成员函数count()红黑树hash容器中有自己的count):

set /multiset
map / multimap
unordered_set / unordered_multiset
unordered_map / unordered_multimap

1、count

  • 区间内有和value相等的元素count + 1
template<class InputIterator, class T>
typename iterator_traits<InputIterator>::difference_type
count(InputIterator first, InputIterator last, const T& value){...}

2、count_if

  • 区间内有符合pred条件的 count + 1
template<class InputIterator, class Predicate>
typename iterator_traits<InputIterator>::difference_type
count_if(InputIterator first, InputIterator last, Predicate pred){...}

find

容器不带成员函数find():

array, vector, list, forward_list, deque

容器带有成员函数find()红黑树hash容器中有自己的find):

set /multiset
map / multimap
unordered_set / unordered_multiset
unordered_map / unordered_multimap

1、find

  • 循序查找,返回第一个和value相等的迭代器
template<class InputIterator, class T>
InputIterator find(InputIterator first, InputIterator last, const T& value){...}

1、find_if

  • 循序查找,查找符合条件的第一个元素的迭代器
template<class InputIterator, class Predicate>
InputIterator find_if(InputIterator first, InputIterator last, Predicate pred){...}

sort

  • sort要求RandomAccessIterator

容器不带成员函数sort():

array, vector, deque

//已经排序,遍历自然形成sorted状态
set /multiset
map / multimap
unordered_set / unordered_multiset
unordered_map / unordered_multimap

容器带有成员函数sort():下面这两种容器都不能 " "

list, forward_list

sort

  • 默认从小到大排序,也可以指定自己的比较函数,可以是仿函数,可以是函数,仿函数必须传入该仿函数的实例。
sort(InputIterator first, InputIterator last, Function f)

binary_search

  • 一定是在已排序状态下
  • 源码中就是调用lower_bound
template<class ForwardIterator, class T>
bool binary_search(ForwardIterator first, ForwardIterator last, const T& value){...}

C++、STL标准模板库和泛型编程 ——迭代器、 算法、仿函数(侯捷)

lower_bound(ForwardIterator first, ForwardIterator last, T target)
upper_bound(ForwardIterator first, ForwardIterator last, T target)

lower_bound二分查找的一个版本,如果找到对应的值,则返回指向其中第一个元素的迭代器,如果不存在,则返回最适合安插这个target的点的迭代器 ,也就是说它返回迭代器指向第一个不小于target的元素,返回的是不破坏排序得以安插target的第一个适当位置。

仿函数 functors(六大部件中最简单的一种!)

  • 只为算法而服务!
  • class模仿函数,必须要重载小括号()

有三大类,大该至少共有24个仿函数:

  • 算术类+-*/ 等);
  • 逻辑运算类&&|| 等);
  • 相对关系类(返回 bool )。

算术类:(Arithmetic)

template<class T>
struct plus : public binary_function<T, T, T>{
	T operator()(const T& x, const T& y) const{
		return x + y;
	}
};

template<class T>
struct minus : public binary_function<T, T, T>{
	T operator()(const T& x, const T& y) const{
		return x - y;
	}
};

...

逻辑运算类:(Logical)

template<class T>
struct logical_and : public binary_function<T, T, bool>{
	bool operator()(const T& x, const T& y) const{
		return x && y;
	}
};

...

相对关系类:(Relational)

template<class T>
struct equal_to : public binary_function<T, T, bool>{
	bool operator()(const T& x, const T& y) const{
		return x == y;
	}
};

template<class T>
struct less : public binary_function<T, T, bool>{
	bool operator()(const T& x, const T& y) const{
		return x < y;
	}
};

...

使用例子:

//using explicitly default comparison(operator <):
sort(myvec.begin(), myvec.end(), less<int>());

为什么要把它们设计成仿函数呢?

  • 因为要传入算法!要把它们作为参数

上面的列举的几个都继承binary_function,说明有两个操作数,为什么要让仿函数继承这些类呢?

  • 首先,继承他们,不会增加仿函数的内存大小;
  • 其次,继承了他们,会有了first_argument_type等的typedef,后续可以根据这个类型进行一些修改。
  • 以及仿函数(functors)的可适配(adaptable)条件:STL规定每个Adaptable Function都应挑选适当地继承unary_fucntionbinary_function等类,才能融入到STL中,才能回答适配器的问题 (就像Traits机要回答迭代器的问题一样)。

unary_fucntionbinary_function的定义:

//一个操作数(例如 对一个值取反)
template<class Arg, class Result>
struct unary_fucntion{//理论上大小为0,(sizeof为1)
	typedef Arg argument_type;
	typedef Result result_type;
}

//两个操作数
template<class Arg1, class Arg2, class Result>
struct bianry_fucntion{//理论上大小为0,(sizeof为1)
	typedef Arg1 first_argument_type;
	typedef Arg2 second_argument_type;
	typedef Result result_type;
}

注:仅供学习参考,如有不足,欢迎指正!

觉得有帮助的话,点个赞呗(^ - ^)!文章来源地址https://www.toymoban.com/news/detail-424308.html

到了这里,关于C++、STL标准模板库和泛型编程 ——迭代器、 算法、仿函数(侯捷)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • STL标准库与泛型编程(侯捷)笔记4

    本文是学习笔记,仅供个人学习使用。如有侵权,请联系删除。 参考链接 Youbute: 侯捷-STL标准库与泛型编程 B站: 侯捷 - STL Github:STL源码剖析中源码 https://github.com/SilverMaple/STLSourceCodeNote/tree/master Github:课程ppt和源码 https://github.com/ZachL1/Bilibili-plus 介绍基于红黑树和hashtable的关联

    2024年01月21日
    浏览(36)
  • STL标准库与泛型编程(侯捷)笔记2

    本文是学习笔记,仅供个人学习使用。如有侵权,请联系删除。 参考链接 Youbute: 侯捷-STL标准库与泛型编程 B站: 侯捷 - STL Github:STL源码剖析中源码 https://github.com/SilverMaple/STLSourceCodeNote/tree/master Github:课程ppt和源码 https://github.com/ZachL1/Bilibili-plus 下面是第二讲的部分笔记:C++标

    2024年01月21日
    浏览(50)
  • STL标准库与泛型编程(侯捷)笔记6(完结)

    本文是学习笔记,仅供个人学习使用。如有侵权,请联系删除。 参考链接 Youbute: 侯捷-STL标准库与泛型编程 B站: 侯捷 - STL Github:STL源码剖析中源码 https://github.com/SilverMaple/STLSourceCodeNote/tree/master Github:课程ppt和源码 https://github.com/ZachL1/Bilibili-plus 下面是C++标准库体系结构与内核

    2024年01月16日
    浏览(40)
  • 10.1 C++ STL 模板适配与迭代器

    STL(Standard Template Library)标准模板库提供了模板适配器和迭代器等重要概念,为开发者提供了高效、灵活和方便的编程工具。模板适配器是指一组模板类或函数,它们提供一种适配机制,使得现有的模板能够适应新的需求。而迭代器则是STL中的令一种重要的概念,它是一个抽

    2024年02月12日
    浏览(29)
  • 31 C++ 模版和泛型

    我们先来看一类问题,如下的三个方法能否换成一个方法呢? 这就是模版的来历。 所谓泛型编程,是以独立于 任何 特定类型的方式编写代码 这意味着,我们在声明或者定义的时候,不会指定具体的类型。 但是在使用的时候,需要指定具体的类型或者值 模版是泛型编程的基

    2024年02月02日
    浏览(27)
  • C++ STL 标准模板库介绍与入门

    目录 1、概述 1.1、C++ 标准库 1.2、Boost库 2、STL 版本 2.1、HP 原始版本

    2024年02月05日
    浏览(46)
  • 一天学完C++的标准模板库STL

    提到字符串,我们会想起C语言中的 char * ,这是一个指针。而在STL中 string 也是用来声明字符串的,但是 string 是一个类,需要导入库 #includestring 。 String 封装了 char * ,管理这个字符串,是一个 char* 型的 容器 。 string 本质上是一个动态的 char 数组。 使用String字符串的第一步

    2023年04月17日
    浏览(31)
  • 【C++】:STL——标准模板库介绍 || string类

    STL(standard template libaray-标准模板库):是C++标准库的重要组成部分,不仅是一个可复用的组件库,而且 是一个包罗数据结构与算法的软件框架 原始版本 Alexander Stepanov、Meng Lee 在惠普实验室完成的原始版本,本着开源精神,他们声明允许任何人任意运用、拷贝、修改、传播、商

    2024年02月05日
    浏览(39)
  • C++——STL标准模板库——容器详解——list

    list:双向链表。list是一种分布式存储的线性表,每个节点分为数据域和指针域,其中指针域中包含一个指向前驱节点的指针和一个指向后续节点的指针,基本模型如下: 1、双向链表:每个元素都有一个前驱和一个后继,这种结构允许在链表的任何位置实现快速的插入和删除

    2024年01月16日
    浏览(34)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包