stl(转)
STL的叫法是“容器”,标准库里面容器不多,数组、链表、红黑树,实现都不负责thread safe、mutable之类,对比下Java的,选择很多,也挺混乱。。
序列容器:动态数组vector,双端队列deque(本质是动态数组加索引),链表list。
关联容器:set,map,multiset,multimap,bitset(叫bit_array更合适)。
容器适配器:stack,queue,priority_queue。
除了bitset,都用到模板,声明大概是这样的:
C++是注重效率的,所以STL很强调一点就是amortized的性能,下面的表很不错,还可以用来速查:
set、map一般会实现成红黑树,这就是它的模板声明里面不需要一个类似Equal_to之类的functor来判断找到Key没有的原因:它调用两次Compare来判断等于。要是不用排序这个特性,只看效率搜索树一般比不过哈希表。
还有一点STL里面iterator是一等公民,主要为了很多算法的效率和封装统一接口,但全是iterator我个人觉得用起来有点不舒服。。
C++0x里面会有基于哈希表的unordered_xxx系列(就是现在很多实现里面的hash_xxx),tuple(Variadic templates)等登场。
template < class Key, class HashFcn = hash<Key>,
class EqualKey = equal_to<Key>, class Allocator = alloc<Key> > class hash_set;
template < class Key, class HashFcn = hash<Key>,
class EqualKey = equal_to<Key>, class Allocator = alloc<Key> > class hash_multiset;
template < class Key, class HashFcn = hash<Key>, class T,
class EqualKey = equal_to<Key>, class Allocator = alloc<pair<const Key, T> > > class hash_map;
template < class Key, class HashFcn = hash<Key>, class T,
class EqualKey = equal_to<Key>, class Allocator = alloc<pair<const Key, T> > > class hash_multimap;
template <class Types> class tuple;
最后,除了容器,<algorithm>也是STL中很重要的部分,其实STL最初做出来是想秀泛型算法的。。
Java里面java.util.Collections类做类似的事情。
References:
http://www.cplusplus.com/reference/stl/
http://www.sgi.com/tech/stl/table_of_contents.html
http://www.stlchina.org/
http://www.stlchina.org/documents/EffectiveSTL/index.html
序列容器:动态数组vector,双端队列deque(本质是动态数组加索引),链表list。
关联容器:set,map,multiset,multimap,bitset(叫bit_array更合适)。
容器适配器:stack,queue,priority_queue。
除了bitset,都用到模板,声明大概是这样的:
STL Standard Containers,点击加号展开!
STL Container Adaptors,点击加号展开!
C++是注重效率的,所以STL很强调一点就是amortized的性能,下面的表很不错,还可以用来速查:
Sequence containers |
Associative containers |
|||||||||
Headers |
<vector> |
<deque> |
<list> |
<set> |
<map> |
<bitset> |
||||
Members |
complex |
|||||||||
constructor |
* |
|||||||||
destructor |
O(n) |
|||||||||
operator= |
O(n) |
|||||||||
iterators |
begin |
O(1) |
||||||||
end |
O(1) |
|||||||||
rbegin |
O(1) |
|||||||||
rend |
O(1) |
|||||||||
capacity |
size |
* |
||||||||
max_size |
* |
|||||||||
empty |
O(1) |
|||||||||
resize |
O(n) |
|||||||||
element access |
front |
O(1) |
||||||||
back |
O(1) |
|||||||||
operator[] |
* |
|||||||||
at |
O(1) |
|||||||||
modifiers |
assign |
O(n) |
||||||||
insert |
* |
|||||||||
erase |
* |
|||||||||
swap |
O(1) |
|||||||||
clear |
O(n) |
|||||||||
push_front |
O(1) |
|||||||||
pop_front |
O(1) |
|||||||||
push_back |
O(1) |
|||||||||
pop_back |
O(1) |
|||||||||
observers |
key_comp |
O(1) |
||||||||
value_comp |
O(1) |
|||||||||
operations |
find |
O(log n) |
||||||||
count |
O(log n) |
|||||||||
lower_bound |
O(log n) |
|||||||||
upper_bound |
O(log n) |
|||||||||
equal_range |
O(log n) |
|||||||||
unique members |
Container Adaptors |
|||||
Headers |
<stack> |
<queue> |
|||
Members |
|||||
constructor |
* |
||||
capacity |
size |
O(1) |
|||
empty |
O(1) |
||||
element access |
front |
O(1) |
|||
back |
O(1) |
||||
top |
O(1) |
||||
modifiers |
push |
O(1) |
|||
pop |
O(1) |
set、map一般会实现成红黑树,这就是它的模板声明里面不需要一个类似Equal_to之类的functor来判断找到Key没有的原因:它调用两次Compare来判断等于。要是不用排序这个特性,只看效率搜索树一般比不过哈希表。
还有一点STL里面iterator是一等公民,主要为了很多算法的效率和封装统一接口,但全是iterator我个人觉得用起来有点不舒服。。
C++0x里面会有基于哈希表的unordered_xxx系列(就是现在很多实现里面的hash_xxx),tuple(Variadic templates)等登场。
template < class Key, class HashFcn = hash<Key>,
class EqualKey = equal_to<Key>, class Allocator = alloc<Key> > class hash_set;
template < class Key, class HashFcn = hash<Key>,
class EqualKey = equal_to<Key>, class Allocator = alloc<Key> > class hash_multiset;
template < class Key, class HashFcn = hash<Key>, class T,
class EqualKey = equal_to<Key>, class Allocator = alloc<pair<const Key, T> > > class hash_map;
template < class Key, class HashFcn = hash<Key>, class T,
class EqualKey = equal_to<Key>, class Allocator = alloc<pair<const Key, T> > > class hash_multimap;
template <class Types> class tuple;
最后,除了容器,<algorithm>也是STL中很重要的部分,其实STL最初做出来是想秀泛型算法的。。
Java里面java.util.Collections类做类似的事情。
References:
http://www.cplusplus.com/reference/stl/
http://www.sgi.com/tech/stl/table_of_contents.html
http://www.stlchina.org/
http://www.stlchina.org/documents/EffectiveSTL/index.html