Differences between string, vector, list, deque, set, and map containers in STL

Source: Internet
Author: User

 

In STL, the basic containers include string, vector, list, deque, set, and map.

Set and map are unordered storage elements. They can only be accessed through the interfaces provided by it;

Set: set, which is used to determine whether an element is in a group and is rarely used;
Map: ing, which is equivalent to a dictionary. It maps a value to another value. If you want to create a dictionary, use it;
The underlying layer uses a tree structure, most of which are implemented using a balanced binary tree (RB-tree). It is a constant time to search for a value, and the traversal effect is good, but every time a value is inserted, the underlying balance binary tree will be re-formed, and the efficiency will be affected.

String, vector, list, deque, and set are ordered containers.

1. String

String is the implementation of basic_string <char>, which is stored continuously in the memory. to improve efficiency, there will be reserved memory, such as string S = "ABCD". In this case, the space used by s may be 255,
When string is added to s again, the memory will not be allocated again. The memory will not be applied again until the content is greater than 255, which improves its performance.
When the content is greater than 255, the string will first allocate a new memory, then copy the content, and then copy the previous content.

String operations, if it is added to the end, generally do not need to allocate memory, so the fastest performance;
If you want to perform intermediate or partial operations, such as adding or deleting elements or replacing elements, You need to perform memory replication to reduce the performance.

If you delete an element, string generally does not release the allocated memory, so that it can be used more efficiently next time.

Because the string has a pre-reserved memory, if you use it in large quantities, there will be a waste of memory, which needs to be considered. There is also a need to consider not to release too much memory when deleting elements.

The memory in the string is allocated in the heap, so the length of the string can be very large, while char [] is allocated in the stack, and the length is limited by the maximum stack length that can be used.

If you know the maximum length of the string to be used, you can use a common char [] instead of a string.
String is used when the length of a string is unknown or is greatly changed.

If the string has been added and deleted multiple times, the current size is much smaller than the maximum size. To reduce the size of the string, you can use:
String S = "abcdefg ";
String y (s); // when the memory is re-allocated, y will only allocate memory larger than the content in S, so the waste will not be very large.
S. Swap (y); // reduce the memory used by S

If there is enough memory, you don't need to consider this.

Capacity is a function used to view the current memory,
You can try to assign a string to the capacity returned value, and return value after other operations.


2. Vector

Vector is a dynamic array. it also allocates memory in the heap. elements are stored continuously and the memory is retained. If the memory size is reduced, the memory will not be released. if the new value is greater than the current size, the memory will be allocated.

It has a continuous memory space and the starting address remains unchanged. Therefore, it supports instant access, that is, the [] operator, but because its memory space is continuous, therefore, insertion and deletion in the middle will cause a copy of the memory block. In addition, when the memory space after the array is insufficient, you need to apply for a large enough memory and copy the memory. These greatly affect the efficiency of vector.

The last element is the fastest to operate (the fastest to add and delete later). In this case, you do not need to move the memory. This is required only when the reserved memory is insufficient;
You need to move the memory to add and delete elements in the middle and the beginning. If your element is a structure or class, the structure and structure operations will be performed while moving the element, therefore, the performance is not high (it is best to put the pointer of the structure or class into the vector, rather than the structure or class itself, so as to avoid the construction and analysis structure when moving ).
In terms of access, the access to any element is O (1), that is, constant, so vector is often used to save the content that requires frequent random access, you do not need to add or delete intermediate elements frequently.

In comparison, we can see that the attributes of the vector are similar to those of the string. You can also use capacity to view the memory currently retained and use swap to reduce its memory usage.

Summary
Use Vector for frequent random access.


3. List

A list is a two-way linked list, where elements are stored in the heap. Each element is stored in a piece of memory. Its memory space can be discontinuous and data access can be performed through pointers, this feature makes access to it very inefficient, so it does not provide a [] OPERATOR overload. However, due to the characteristics of the linked list, it supports deletion and insertion anywhere with good efficiency.

List has no space reserved habits, so every allocation of an element will be allocated from the memory, Each deletion of an element will release the memory it occupies.

The performance of adding and deleting elements to a list is high, and the memory does not need to be moved. Of course, every element does not need to be constructed or parsed. Therefore, it is often used as a random operation container.
However, when accessing the elements in the list, the first and last access is the fastest.
O (n) is used to access other elements, so it is better to use other elements if frequent random access is required.

Summary
If you like to add and delete large objects frequently, use list;
The object to be saved is not large, and the structure and destructor are not complex. You can use vector instead;
List <pointer> is the best way to achieve the lowest performance. In this case, it is better to use vector <pointer> because the pointer is not constructed or destructed, and does not occupy a large amount of memory.


4. deque

Deque is a double-ended queue and stores content in the heap as follows:
[Heap 1]
...
[Heap 2]
...
[Heap 3]

Each heap stores several elements, and there is a pointer between the heap and the heap. It looks like a combination of list and vector, but it does;
Deque allows you to quickly add or delete elements in the front, or quickly add or delete elements in the back, and then provide a relatively high random access speed;
It supports the [] operator, that is, instant access, allowing you to quickly add and delete elements or add and delete elements later, then there can be a relatively high random access speed, which is almost the same as the efficiency of vector. It supports operations at both ends: push_back, push_front, pop_back, pop_front, etc, in addition, the operation efficiency on both ends is similar to that on list.
In the standard library, vector and deque provide almost the same interface. In terms of structure, they differ mainly in the differences between the two containers in the organizational memory, deque allocates memory by page or block. Each page contains a fixed number of elements. on the contrary, the vector allocates a continuous memory. The vector is only effective when an element is inserted at the end of the sequence, the deque paging organization method provides insert and erase operations with constant time even at the front end of the container, and is more efficient than vector in terms of volume growth.

Summary:
Vector allows you to quickly add and delete elements at the end and quickly access any element;
List allows you to quickly add and delete elements in all places, but you can only quickly access the first and last elements;
Deque is as fast as adding elements at the beginning and end, and provides random access methods. Like a vector, deque uses [] to access any element, but random access speed is not as fast as that of vector, because it needs to handle heap redirection internally;
Deque also has reserved space. in addition, because deque does not require continuous space, the elements that can be stored are larger than those of vector. in addition, when adding elements to the front and back, you do not need to move the elements of other blocks, so the performance is also high.

Therefore, in actual use, how to select one of the three containers should be determined according to your needs. Generally, the following principles should be followed:
1. If you need efficient instant access without worrying about the efficiency of insertion and deletion, use vector;
2. If you need to insert and delete a large number of objects without having to access them immediately, you should use list;
3. If you need immediate access and care about data insertion and deletion at both ends, use deque.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.