containers <-----> iterators <--------> algorithms
存放data 访问data 操作data
container:data structure 用于存放data
特点:
内存自动管理;
使用值语义,即容器中存放的是单独一个副本:所以容器虽然是Template实现的,但还是有一些限制(copy,=)
实现了常用的data structure:不同数据结构特点不同,注意选择
提供iterator接口:因底层数据结构不同,故提供的iterator类型不同 (vector和deque提供RandomACcess iterator,其余提供Bidirectional iterator)
container分类:(查阅www.cplusplus.com)
分为Sequence Containers和Associative Containers。
Amortized complexity shown. Legend: O(1) constant < O(log n) logarithmic < O(n) linear; *=depends on container
还有container adapters:stack,queue,priority_queue
iterator:屏蔽底层容器的内部数据结构的差别,提供一个统一的接口来访问容器中的数据,
使得algorithm和container分开,提供灵活性。
iterator分类:类型代表能提供的功能。
一些非常有用的iterator:
1. Inserters: 可以有效防止容器空间不足
back_inserter | Construct a back insert iterator (function template) |
inserter | Construct an insert iterator (function template) |
2.
Input/Output iterators:将IO当成container一样来访问
3. Reverse iterator: 使得算法可以不用任何修改就对容器进行逆序操作
通过容器的rbegin和rend获取
algorithms:
操作data
特点:
不直接操作container,而是通过iterator。注意算法要求的iterator类型。
通常是通过iterator操作container中的某个半开半闭区间 [ )
不修改container的容量,即不删除和插入(除非使用inserter)。那些modify的操作,都是通过改变容器内元素的顺序来实现。
注意是否该使用container自带的成员函数,还是通用的algorithm
有些algorithm接收函数作为参数,这样可以增加算法的灵活性。
function objects:在其class中重载operator()。比函数更好,因为function object是有类型的,且内部能保存状态。
STL提供的一些function object相关的功能:
Operator classes
Arithmetic operations:
plus | Addition function object class (class template) |
minus | Subtraction function object class (class template) |
multiplies | Multiplication function object class (class template) |
divides | Division function object class (class template) |
modulus | Modulus function object class (class template ) |
negate | Negative function object class (class template) |
Comparison operations:
equal_to | Function object class for equality comparison (class template ) |
not_equal_to | Function object class for non-equality comparison (class template ) |
greater | Function object class for greater-than inequality comparison (class template ) |
less | Function object class for less-than inequality comparison (class template ) |
greater_equal | Function object class for greater-than-or-equal-to comparison (class template ) |
less_equal | Function object class for less-than-or-equal-to comparison (class template ) |
Logical operations:
logical_and | Logical AND function object class (class template ) |
logical_or | Logical OR function object class (class template ) |
logical_not | Logical NOT function object class (class template ) |
Adaptor and conversion functions
Negators
not1 | Return negation of unary function object (function template) |
not2 | Return negation of binary function object (function template) |
Parameter binders
bind1st | Return function object with first parameter bound (function template ) |
bind2nd | Return function object with second parameter bound (function template ) |
Conversors
ptr_fun | Convert function pointer to function object (function template) |
mem_fun | Convert member function to function object (pointer version) (function template) |
mem_fun_ref | Convert member function to function object (reference version) (class template) |