C ++ STL basic container string, vector, list, deque, map ..

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, 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.

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, no memory will 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.
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. You only need to retain the memory when it 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
Please use vector for frequent Random Access

3. List

A list is a linked list, and elements are stored in the heap. Each element is stored in a memory.
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, which is different from the above, you can see

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

The dual-end queue stores the 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 high random access speeds.

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, other block elements do not need to be moved, so the performance is high.

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.