打酱油的萧萧

一个打酱油的程序员
[转]log4cxx以时间,大小的备份
[转]C++中的内存对齐

STL容器介绍

萧萧 posted @ 2010年8月08日 05:45 in C/C++ with tags c++ STL map Set list , 2414 阅读

STL的容器可以分为以下几个大类: 

一:序列容器:有vector, list, deque, string。

二:关联容器: 有set, multiset, map, mulmap, hash_set, hash_map, hash_multiset, hash_multimap。

三:其他的杂项:stack, queue, valarray, bitset。

 

STL各个容器的实现:

(1) vector

内部数据结构:数组。

随机访问每个元素,所需要的时间为常量。

在末尾增加或删除元素所需时间与元素数目无关,在中间或开头增加或删除元素所需时间随元素数目呈线性变化。

可动态增加或减少元素,内存管理自动完成,但程序员可以使用reserve()成员函数来管理内存。

vector的迭代器在内存重新分配时将失效(它所指向的元素在该操作的前后不再相同)。当把超过capacity()-size()个元素插入vector中时,内存会重新分配,所有的迭代器都将失效;否则,指向当前元素以后的任何元素的迭代器都将失效。当删除元素时,指向被删除元素以后的任何元素的迭代器都将失效。

 

(2)deque

内部数据结构:数组。随机访问每个元素,所需要的时间为常量。在开头和末尾增加元素所需时间与元素数目无关,在中间增加或删除元素所需时间随元素数目呈线性变化。可动态增加或减少元素,内存管理自动完成,不提供用于内存管理的成员函数。增加任何元素都将使deque的迭代器失效。在deque的中间删除元素将使迭代器失效。在deque的头或尾删除元素时,只有指向该元素的迭代器失效。

 

(3)list

内部数据结构:双向环状链表。不能随机访问一个元素。可双向遍历。在开头、末尾和中间任何地方增加或删除元素所需时间都为常量。可动态增加或减少元素,内存管理自动完成。增加任何元素都不会使迭代器失效。删除元素时,除了指向当前被删除元素的迭代器外,其它迭代器都不会失效。

 

(4)slist

内部数据结构:单向链表。不可双向遍历,只能从前到后地遍历。其它的特性同list相似。

 

(5)stack

适配器,它可以将任意类型的序列容器转换为一个堆栈,一般使用deque作为支持的序列容器。元素只能后进先出(LIFO)。不能遍历整个stack。

 

(6)queue

适配器,它可以将任意类型的序列容器转换为一个队列,一般使用deque作为支持的序列容器。元素只能先进先出(FIFO)。不能遍历整个queue。

 

(7)priority_queue

适配器,它可以将任意类型的序列容器转换为一个优先级队列,一般使用vector作为底层存储方式。只能访问第一个元素,不能遍历整个priority_queue。第一个元素始终是优先级最高的一个元素。

 

(8)set

键和值相等。键唯一。元素默认按升序排列。如果迭代器所指向的元素被删除,则该迭代器失效。其它任何增加、删除元素的操作都不会使迭代器失效。

 

(9)multiset

键可以不唯一。其它特点与set相同。

 

(10)hash_set

与set相比较,它里面的元素不一定是经过排序的,而是按照所用的hash函数分派的,它能提供更快的搜索速度(当然跟hash函数有关)。其它特点与set相同。

 

(11)hash_multiset

键可以不唯一。其它特点与hash_set相同。

 

(12)map

键唯一。元素默认按键的升序排列。如果迭代器所指向的元素被删除,则该迭代器失效。其它任何增加、删除元素的操作都不会使迭代器失效。

 

(13)multimap

键可以不唯一。其它特点与map相同。

 

(14)hash_map

与map相比较,它里面的元素不一定是按键值排序的,而是按照所用的hash函数分派的,它能提供更快的搜索速度(当然也跟hash函数有关)。其它特点与map相同。

 

(15)hash_multimap

键可以不唯一。其他特点与hash_map相同。

 

happy room 说:
2018年3月29日 17:41

可动态增加或减少元素,内存管理自动完成,但程序员可以使用reserve()成员函数来管理内存。!

happy wheels 说:
2018年5月09日 16:31

它里面的元素不一定是经过排序的

happy wheels 说:
2018年8月06日 15:41

I have read through some similar topics! However, your post has given me a very special impression, unlike other posts. I hope you continue to have valuable articles like this or more to share with everyone!

Papas Games 说:
2018年8月28日 10:46

Summer is easy with heavy rain but I like it. It's wonderful to have rainbows after rain, and I watch it until it disappears. It seems to be free but it helps me feel comfortable

asianfanfics 说:
2018年9月25日 18:24

I have read through some similar topics! However, your post has given me a very special impression, unlike other posts. I hope you continue to have valuable articles like this or more to share with everyone!

slither io 说:
2018年10月11日 17:49

great, thank you for sharing information

yahoo mail login 说:
2019年4月10日 17:49

Thank you for sharing the helpful article. I'm glad to find it hear.

aos tv world cup 说:
2019年5月27日 16:21

Download AOS TV Apk for Android devices and FireStick devices to watch live tv and live sports.

FB Downloader Guide 说:
2019年6月06日 15:10

Thank you for your post, I look for such article along time, today i find it finally. this post give me lots of advise it is very useful for me

Ashley D. Rabideau 说:
2019年7月16日 00:54

I would say your article is so interesting and informative for me and this article explained everything in detail. You have done a superb job thanks for sharing this kind of stuff with us.

Eddie A Gallegos 说:
2019年7月24日 17:44

Really very happy to say, your post is very interesting to read.I never stop myself to say something about it. You’re doing a great job. Keep it up, I have found a way to get a Free iTunes Gift Card after a long research.

valentine day specia 说:
2020年1月20日 15:23

Download best love short video for valentine day special whatsapp status video

nivedita 说:
2020年4月27日 16:03

Trust because you are willing to accept the risk, not because it's safe or certain.Download this link.
<a href="https://whatsappstatuses.xyz/category/motivational/
">inspirational whatsapp status video download</a>

run 3 说:
2020年7月10日 15:15

As usual, great post. You keep amazing me with your content design and layout. And thanks for sharing this awesome piece

run 3 说:
2020年7月10日 15:17

As usual, great post. You keep amazing me with your content design and layout. And thanks for sharing this awesome piece

NCERT Urdu Sample Pa 说:
2022年9月28日 13:41

Urdu is a language which was introduced in the NCERT Syllabus for all Urdu Medium and Urdu Spoken students. Some of the students who are studying the 2nd class at central education boards such as CBSE, KVS, NCERT Urdu Sample Paper Class 2 JNV and others have chosen Urdu as their mother language and some of chosen it’s their Second or Third Language.Subject experts of the National Council for Education Research and Training have designed and published the Ibtedai study and learning material for all regional students as per the newly revised syllabus and curriculum of the course. All Lessons practice questions are introduced chapter by chapter for all topics of the course to the Term-1 & Term-2 exams.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter