惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Stack Overflow Blog
Stack Overflow Blog
V
V2EX
爱范儿
爱范儿
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 司徒正美
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
Tailwind CSS Blog
The Last Watchdog
The Last Watchdog
S
Secure Thoughts
S
Security Archives - TechRepublic
阮一峰的网络日志
阮一峰的网络日志
人人都是产品经理
人人都是产品经理
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Application and Cybersecurity Blog
Application and Cybersecurity Blog
美团技术团队
S
Security Affairs
AI
AI
SecWiki News
SecWiki News
Hugging Face - Blog
Hugging Face - Blog
B
Blog
小众软件
小众软件
Cloudbric
Cloudbric
V
Visual Studio Blog
PCI Perspectives
PCI Perspectives
博客园 - 【当耐特】
Microsoft Azure Blog
Microsoft Azure Blog
Schneier on Security
Schneier on Security
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Vercel News
Vercel News
N
News and Events Feed by Topic
博客园 - 叶小钗
Last Week in AI
Last Week in AI
V
Vulnerabilities – Threatpost
L
LINUX DO - 热门话题
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Google Online Security Blog
Google Online Security Blog
TaoSecurity Blog
TaoSecurity Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
S
SegmentFault 最新的问题
F
Full Disclosure
Martin Fowler
Martin Fowler
L
Lohrmann on Cybersecurity
博客园 - 三生石上(FineUI控件)
T
The Blog of Author Tim Ferriss
www.infosecurity-magazine.com
www.infosecurity-magazine.com
P
Privacy & Cybersecurity Law Blog
G
GRAHAM CLULEY
Attack and Defense Labs
Attack and Defense Labs
WordPress大学
WordPress大学
博客园 - Franky

博客园 - y轴

Apache服务器配置技巧(转) 网站架构收集 (转) 谈谈网站静态化(转) 经典SQL语句集锦(转) Linux中find常见用法示例(转) linux 进程间通信(转) stl总结(转) 网络编程指南(转) Linux 套接字编程中的 5 个隐患(转) pthread 常用函数 linux socket 常用函数 MySQL与事务(转) 在PHP中实现进程间通讯 (转) mysql storge engine(转) apache Order Deny 的配置 apache rewrite原理与使用(转) 利用apache代理功能实现负载均衡的集群(转) HTTP Referer二三事 (转) 非捕获,前瞻与后顾(转)
stl(转)
y轴 · 2008-08-28 · via 博客园 - y轴

STL的叫法是“容器”,标准库里面容器不多,数组、链表、红黑树,实现都不负责thread safe、mutable之类,对比下Java的,选择很多,也挺混乱。。

序列容器:动态数组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

vector

deque

list

set

multiset

map

multimap

bitset


constructor

*

constructor

constructor

constructor

constructor

constructor

constructor

constructor

constructor

destructor

O(n)

destructor

destructor

destructor

destructor

destructor

destructor

destructor


operator=

O(n)

operator=

operator=

operator=

operator=

operator=

operator=

operator=

operators

iterators

begin

O(1)

begin

begin

begin

begin

begin

begin

begin


end

O(1)

end

end

end

end

end

end

end


rbegin

O(1)

rbegin

rbegin

rbegin

rbegin

rbegin

rbegin

rbegin


rend

O(1)

rend

rend

rend

rend

rend

rend

rend


capacity

size

*

size

size

size

size

size

size

size

size

max_size

*

max_size

max_size

max_size

max_size

max_size

max_size

max_size


empty

O(1)

empty

empty

empty

empty

empty

empty

empty


resize

O(n)

resize

resize

resize






element access

front

O(1)

front

front

front






back

O(1)

back

back

back






operator[]

*

operator[]

operator[]




operator[]


operator[]

at

O(1)

at

at







modifiers

assign

O(n)

assign

assign

assign






insert

*

insert

insert

insert

insert

insert

insert

insert


erase

*

erase

erase

erase

erase

erase

erase

erase


swap

O(1)

swap

swap

swap

swap

swap

swap

swap


clear

O(n)

clear

clear

clear

clear

clear

clear

clear


push_front

O(1)


push_front

push_front






pop_front

O(1)


pop_front

pop_front






push_back

O(1)

push_back

push_back

push_back






pop_back

O(1)

pop_back

pop_back

pop_back






observers

key_comp

O(1)




key_comp

key_comp

key_comp

key_comp


value_comp

O(1)




value_comp

value_comp

value_comp

value_comp


operations

find

O(log n)




find

find

find

find


count

O(log n)




count

count

count

count

count

lower_bound

O(log n)




lower_bound

lower_bound

lower_bound

lower_bound


upper_bound

O(log n)




upper_bound

upper_bound

upper_bound

upper_bound


equal_range

O(log n)




equal_range

equal_range

equal_range

equal_range


unique members


capacity
reserve


splice
remove
remove_if
unique
merge
sort
reverse





set
reset
flip
to_ulong
to_string
test
any
none

 

Container Adaptors

Headers

<stack>

<queue>

Members

stack

queue

priority_queue


constructor

*

constructor

constructor

constructor

capacity

size

O(1)

size

size

size

empty

O(1)

empty

empty

empty

element access

front

O(1)


front


back

O(1)


back


top

O(1)

top


top

modifiers

push

O(1)

push

push

push

pop

O(1)

pop

pop

pop

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