C++ STL 总结

1 篇文章 0 订阅
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。

 Sequence containersAssociative containers 
Headers<vector><deque><list><set> <bitset>
Memberscomplexvectordequelistsetmultisetmapmultimapbitset
 constructor*constructorconstructorconstructorconstructorconstructorconstructorconstructorconstructor
destructorO(n)destructordestructordestructordestructordestructordestructordestructor 
operator=O(n)operator=operator=operator=operator=operator=operator=operator=operators
iteratorsbeginO(1)beginbeginbeginbeginbeginbeginbegin 
endO(1)endendendendendendend 
rbeginO(1)rbeginrbeginrbeginrbeginrbeginrbeginrbegin 
rendO(1)rendrendrendrendrendrendrend 
capacitysize*sizesizesizesizesizesizesizesize
max_size*max_sizemax_sizemax_sizemax_sizemax_sizemax_sizemax_size 
emptyO(1)emptyemptyemptyemptyemptyemptyempty 
resizeO(n)resizeresizeresize     
element accessfrontO(1)frontfrontfront     
backO(1)backbackback     
operator[]*operator[]operator[]   operator[] operator[]
atO(1)atat      
modifiersassignO(n)assignassignassign     
insert*insertinsertinsertinsertinsertinsertinsert 
erase*eraseeraseeraseeraseeraseeraseerase 
swapO(1)swapswapswapswapswapswapswap 
clearO(n)clearclearclearclearclearclearclear 
push_frontO(1) push_frontpush_front     
pop_frontO(1) pop_frontpop_front     
push_backO(1)push_backpush_backpush_back     
pop_backO(1)pop_backpop_backpop_back     
observerskey_compO(1)   key_compkey_compkey_compkey_comp 
value_compO(1)   value_compvalue_compvalue_compvalue_comp 
operationsfindO(log n)   findfindfindfind 
countO(log n)   countcountcountcountcount
lower_boundO(log n)   lower_boundlower_boundlower_boundlower_bound 
upper_boundO(log n)   upper_boundupper_boundupper_boundupper_bound 
equal_rangeO(log n)   equal_rangeequal_rangeequal_rangeequal_range 
unique members capacity
reserve
 splice
remove
remove_if
unique
merge
sort
reverse
    set
reset
flip
to_ulong
to_string
test
any
none
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_inserterConstruct a back insert iterator (function template)
front_inserterConstructs a front insert iterator (function template)
inserterConstruct an insert iterator (function template)
2.  Input/Output iterators:将IO当成container一样来访问
istream_iteratorIstream iterator (class template)
ostream_iteratorOstream iterator (class template )
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:
plusAddition function object class (class template)
minusSubtraction function object class (class template)
multipliesMultiplication function object class (class template)
dividesDivision function object class (class template)
modulusModulus function object class (class template )
negateNegative function object class (class template)

Comparison operations:
equal_toFunction object class for equality comparison (class template )
not_equal_toFunction object class for non-equality comparison (class template )
greaterFunction object class for greater-than inequality comparison (class template )
lessFunction object class for less-than inequality comparison (class template )
greater_equalFunction object class for greater-than-or-equal-to comparison (class template )
less_equalFunction object class for less-than-or-equal-to comparison (class template )

Logical operations:
logical_andLogical AND function object class (class template )
logical_orLogical OR function object class (class template )
logical_notLogical NOT function object class (class template )

Adaptor and conversion functions

Negators
not1Return negation of unary function object (function template)
not2Return negation of binary function object (function template)

Parameter binders
bind1stReturn function object with first parameter bound (function template )
bind2ndReturn function object with second parameter bound (function template )

Conversors
ptr_funConvert function pointer to function object (function template)
mem_funConvert member function to function object (pointer version) (function template)
mem_fun_refConvert member function to function object (reference version) (class template)
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值