C++ STL container concept

string container:

string其实相当于一个保存字符的序列容器,因此除了有字符串的一些常用操作以外,还有包含了所有的序列容器的操作。字符串的常用操作包括:增加、删除、修改、查找比较、链接、输入、输出等

vector container: vector

​ is a sequential container that encapsulates a dynamic size array, which can store any type of dynamic array.

​ The physical address and the logical address are continuous, which can achieve fast access (first address + i direct access)

   1)可以直接访问任何元素。

   2)线性顺序结构。可以指定一块连续的空间,也可以不预先指定大小,空间可自动扩展,也可以像数组一样被操作,即支持[ ]操作符和vector.at(),因此可看做动态数组,通常体现在追加数据push_back()和删除末尾数据pop_back()。

   3)当分配空间不够时,vector会申请一块更大的内存块(以2的倍数增长),然后将原来的数据拷贝到新内存块中并将原内存块中的对象销毁,最后释放原来的内存空间。因此如果vector保存的数据量很大时会很消耗性能,因此在预先知道它大小时性能最优。

   4)节省空间。因为它是连续存储,在存储数据的区域是没有浪费的,但实际上大多数时候是存不满的,因此实际上未存储的区域是浪费的。

   5)在内部进行插入和删除的操作效率低。由于vector内部按顺序表结构设计,因此这样的操作基本上是被禁止的,它被设计成只能在后端进行追加和删除操作。

The difference between vector and array:

​ 1. The array can only be expanded manually, and the vector can be expanded automatically

​ 2. After the array inserts the data, the data must be moved backward, and the vector can be inserted at any position

Function Meaning:

​ 1.push_back adds a piece of data at the end of the array

​ 2.pop_back remove the last data of the array

​ 3.at get the data of the numbered position

​ 4.begin to get the pointer of the array head

​ 5.end gets the pointer of the last element of the array + 1

​ 6. front gets a reference to the array head

​ 7.back to get a reference to the last element of the array

​ 8.max_size gets the maximum vector size

​ 9.capacity The size of the current vector allocation

​ 10.size The size of the currently used data

​ 11.resize changes the size of the currently used data, if it is larger than the currently used one, fill the default value

​ 12.reserve changes the size of the space allocated by the current vecotr

​ 13.erase delete the data item pointed to by the pointer

​ 14.clear clear the current vector

​ 15.rbegin returns the start pointer after the vector is reversed (in fact, the original end-1)

​ 16.rend returns the end pointer of the vector reverse structure (in fact, the original begin-1)

​ 17.empty judge whether the vector is empty

​ 18.swap exchange data with another vector

deque container: deque

   1)是一种优化了的、对序列两端进行添加和删除操作、较快速地随机访问的基本序列容器。

   2)采用多个连续的存储块保存对象,并在一个映射结构中保存对这些块及其顺序的跟踪。由于不需要重新分配空间,因此追加元素时比vector更有效。实际上内部有一个map指针。

   3)支持随机访问,即支持[ ]操作符和.at(),但性能不如vector。

   4)可以进行内部随机插入和删除,但性能不如list。

list container: double-linked list

1)线性链表结构。
2)其数据由若干个节点构成,每个节点包括一个信息块(即实际存储的数据)、一个前驱指针和一个后驱指针。无需分配指定的内存大小且可任意伸缩,因此它存储在非连续的内存空间中,并且由指针将有序的元素链接起来。因而相比vector它也占更多的内存。

   3)根据其结构可知随机检索的性能很差,vector是直接找到元素的地址,而它需要从头开始按顺序依次查找,因此检索靠后的元素时非常耗时。即不支持[ ]操作符和.at()。

   4)由于list每个节点保存着它在链表中的位置,插入或删除一个元素仅对最多三个元素有所影响,因此它可以迅速在任何节点进行插入和删除操作。

Associative container set, multiset, map, multimap

The associative container is a binary tree structure, which is sorted according to the characteristics of the elements, and the iterator can get the elements "sequentially" according to the characteristics of the elements. It saves data in the form of key-values, that is, associating keywords and values ​​to save, while the sequential container can only save one type (it can be considered that it only saves keywords, or it can only save values). The underlying implementation is Red-black tree.

Collection set:

   1)快速查找,不允许重复值。

   2)按一定顺序排列,集合中的每个元素被称作集合中的实例。

   3)内部通过链表的方式组织,因此插入的时候比vector快,但在查找和末尾追加比vector慢。

map:

   1)提供一种“键-值”关系的一对一的数据存储能力。键按一定顺序排列且不可重复(set也可以看成没有键只有值的特殊map形式)。

   2)链表方式存储,继承了链表的优缺点。

   3)一对多映射,基于关键字快速查找。

multiset和multimap:

The element is not required to be unique, others are the same as above

Features of associative containers:

   1)红黑树的结构原理。

   2)set和map保证了元素的唯一性,mulset和mulmap扩展了这一属性,可以允许元素不唯一。

   3)元素是有序的集合,默认在插入的时候按升序排列。

   4)插入和删除操作比vector快,比list慢。因为vector是顺序存储,而关联容器是链式存储;而同为链式结构,list是线性,而关联容器是排序的二叉树结构,因此每次都需要对元素重新排序,涉及到的元素变动更多。

   5)对元素的检索操作比vector慢,比list快很多。vector是顺序的连续存储,这是最快的速度;而list需要逐个搜索,搜索时间与容器大小成正比,关联容器查找的复杂度log(n),因此容器越大,关联容器相对list越能体现其优越性。

   6)在使用上set区别于顺序容器的是查询上虽然慢于vector,但却强于list。

   7)在使用上map的功能是不可取代的,它保存了“键-值”关系的数据,而这种键值关系采用了类数组的方式。数组是用数字类型的下标来索引元素的位置,而map是用字符型关键字来索引元素的位置。在使用上map也提供了一种类数组操作的方式,即它可以通过下标来检索数据。在STL中只有vector和map可以通过类数组的方式操作元素。

Guess you like

Origin blog.csdn.net/Gunanhuai/article/details/114640312