算法的形式
-容器container 是个class template
-算法algorithm 是个function template
算法是要来处理容器中的data,但是算法看不到容器只能通过迭代器获取参数
算法向迭代器索取data,迭代器从容器中取得data提供给算法
算法在处理data时会需要知道data容器的形式,这由迭代器告诉算法
算法发出提问迭代器无法回答的时候编译失败

迭代器的分类 (category)
迭代器是要由容器来提供的,根据容器中数据移动特性不同而不同
五种结构为五种不同类型的迭代器,可以被封装成对象


编译过后,编译器会在变量名前后加上其他附加字符作为变量标号

不同标准库版本的做法并不相同,但是接口必须一致

在有些继承关系中的父类只做了一些类型的定义,没有data和function,这时候子类可以继承父类的各种类型

迭代器分类 (category) 对算法的影响
distance算法接受两个参数,计算两个指针参数之间的距离
算法的精细可以大大提高操作的效率
在算法中对不同迭代器进行分类操作,对不同的结构进行不同的操作可以提高运行速度
而标准库中的算法已经很精细地写出了各种分类的操作
对标准库的信任和使用非常重要
算法对迭代器没有强制要求,只有暗示,建议使用会使效率更高的迭代器





算法源代码剖析 (11个例子)
c算法和c++算法的区别,是否以函数模板形式写出来的

accumulate累计算法
有两个版本
-每次将数值累加
-每次将元素做第三个参数所给出的累次操作,一般情况第三个参数所给的是一个可被调用的可以函数也可以是仿函数

for_each算法
对容器中的元素进行依次操作
以一个for循环对每个元素进行第三个参数的操作
也可以使用c++11新形式range-based for statement

replace、replace_if、replace_copy算法
取代算法,修改所有key value
条件取代算法,修改所有符合判定式子的value
拷贝取代算法,在新空间中拷贝元素,修改新空间所有key value

count、count_if算法
计数算法,对范围内所有元素计数
条件计数算法,对范围内复合条件的元素计数
在有些容器中,存在成员函数count,这时候要使用自己的count而不能使用全局count
成员函数中没有count的容器才可以使用全局count

find、find_if算法
查找算法,顺序查找算法
条件查找算法,附条件的顺序查找算法
在有些容器中,存在成员函数find,这时候要使用自己的find而不能使用全局find
成员函数中没有find的容器才可以使用全局find

sort算法
排序算法
在有些容器中,存在成员函数sort,这时候要使用自己的count而不能使用全局sort
成员函数中没有sort的容器才可以使用全局sort

reverse iterator
逆序迭代器,rbegin()和rend()
迭代器,逆向指向容器头尾

binary search算法
二分查找算法
要先排序,再查找

仿函数/函数对象
functors仿函数,六大部件中最简单的一种
当算法需要一些独特的准则时,需要一些特定的操作,这时候要用一般函数或者仿函数来实现
三大类
-算数类
-逻辑运算类
-相对关系类
仿函数要继承才能与STL符合




存在多种Adapter
适配器,六大部件的最后一个
改造接口,不改变主要功能
以复合方式实现功能


Binder2nd
function adapter函数适配器

not1

bind


reverse_iterator

inserter

ostream_iterator

istream_iterator


.
网友评论