Boolan STL 第五周
语言层面,STL中算法是function template,其他的容器、迭代器、仿函数、适配器、分配器都是class template
由于algorithms看不到container,所以iterator必须能提供给algorithms提出问题的答案。
iterator的5种类型:

array、vector、deque属于random_access_iterator_tag
list、rb_tree(set/map等)属于bidirectional_iterator_tag
forward_list、hashtable(unordered set/map等)属于forward_iterator_tag
istream_iterator属于input_iterator_tag
ostream_iterator属于output_iterator_tag
iterator_category对算法的影响:可以通过使用萃取机识别出不同的类别的iterator来重载函数的不同实现。如:distance()函数

iterator_category配合type traits可以使算法针对不同容器的迭代器产生不同的实现,如copy()

算法源码中对应该使用哪种类型的iterator有暗示:

通过给算法传入自定义functor实现特定功能:如accumulate()

算法count/count_if:

算法find/find_if:

算法sort使用:

算法binary_search:必须先排序,才能进行二分搜寻

仿函数functor:只为算法服务,必须要根据参数数量继承unary_function或binary_function才能算可适配的(adaptable),因为他提供了一些会被算法问到的typedefine


适配器:根据要改造的东西分为container adapter、iterator adapter、functor adapter,适配器和被改造的原型是combination的关系。
iterator和functor能提供算法回答所需的typedefine:

函数适配器:改造functor。如:bind2nd、not1


c++11新适配器bind用法:

迭代器适配器:改造iterator,如reverse_iterator、inserter


x适配器:如istream_iterator和ostream_iterator

