C + + STL basic container comparison __c++

Source: Internet
Author: User
Tags reserved
C + + STL basic container Comparison
The basic containers in STL are: string, vector, list, deque, set, map


Both set and map are unordered save elements that can only be accessed through the interfaces it provides


Set: A collection that is used to determine whether an element is in a group, using less
Map: Mapping, equivalent to a dictionary, mapping a value to another value, if you want to create a dictionary use it well


string, vector, list, deque, set are ordered containers
1.string


A string is an basic_string<char> implementation that is stored continuously in memory. To improve efficiency, there will be reserved memory, such as String s= "ABCD", when s uses the space may be 255, When a string adds content to s again, it does not allocate memory again. The memory is not applied again until the content is >255, thereby improving its performance.
When the content is >255, string assigns a new memory first, then copies the contents to the previous content.


The operation of string, if it is added to the last, generally do not need to allocate memory, so the fastest performance;
If you are in the middle or beginning part of the operation, such as to add elements or delete elements, or replace elements, then need to do memory replication, performance will be reduced.


If you delete an element, a string generally does not release the memory it has allocated, in order to be more efficient the next time it is used.


Because string has reserved memory, there is a waste of memory if used in large quantities, which needs to be considered. There is also the need to consider when deleting an element without releasing too much memory.


The memory in string is allocated in the heap, so the length of the string can be very large, whereas 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 you want to use, you can use the normal char[] to implement without using a string.
String used in the case of unknown string length or a large change.


If a string has undergone multiple additions and deletions, now is a much smaller size than the maximum size, and you want to reduce the size used by string, you can use:
string s = "ABCDEFG";
String y (s); Because when you allocate memory again, Y will only allocate memory larger than the content in S, so the waste is not very big
S.swap (y); Reduce the memory used by s


If there's enough memory, you don't have to think about it.
Capacity is a function to view the memory now used
You can try it. String allocates a string of capacity return values, and the return value after other operations


2.vector


Vectors are dynamic arrays. It also allocates memory in the heap, the elements are stored continuously, there is reserved memory, and if the size is reduced, memory will not be released. Memory is redistributed if new value > Current size
The fastest for the last element (the fastest to add deletes later), the general need not to move memory, only if the memory is not enough
Add delete elements to the middle and start operations need to move the memory, if your element is structure or class, then the move will also be constructed and destructors, so performance is not high.
In terms of access, access to any element is O (1), which is constant, so vectors are often used to save content that needs to be randomly accessed frequently, and there is no need to frequently add deletions to intermediate elements.


By comparison, you can see that the vector's properties are similar to string, and you can use capacity to see the current reserved memory and use swap to reduce the memory it uses.


Summarize
Require regular random access use vector
3.list


The list is a list, and the elements are stored in the heap, and each element is placed in a piece of memory
List has no space to reserve the habit, so each allocation of an element will be allocated from memory, each delete an element will release the memory it occupies, which is different from the above, you can watch


list where to add delete element performance is very high, do not need to move memory, of course, do not need to each element is structured and destructor, so often used to do random operation container.
But accessing the elements in the list starts and ends with the fastest
Access to other elements is O (n), so if you need to access it randomly, use the other good


Summarize
If you like to often add delete large objects, then use the list
To save an object that is not very large and is not complex to construct and destructor, you can use vector instead
The list< pointer > is entirely the lowest performing practice, in which case it is better to use the vector< pointer > because the pointer is not constructed and destructor, nor does it occupy a large amount of memory
4.deque


A two-terminal queue that also holds content in the heap. It is saved in the following form:


[Heap 1]
...
[Heap 2]
...
[Heap 3]


Each heap holds several elements, and then there is a pointer between the heap and the heap, which looks like a combination of list and vector, but it does.
Deque allows you to quickly add deletion elements to the front, or quickly add deletion elements later, and then you can have a higher random access speed


Vector is a quick way to add a deletion element at the end, and to quickly access any element
List is a quick way to add delete elements everywhere, but only quickly access the first and last elements
Deque is as fast as adding elements at the beginning and end, and provides a random access method that uses [] to access any element like a vector, but the random access speed is not as fast as the vector, because it wants to process the heap jumps internally
Deque also has reserved space. In addition, because the deque does not require contiguous space, the elements that can be saved are larger than vectors, and this should be noted. There is also the ability to add elements to the front and back without having to move elements of other blocks, so performance is also high
Related Article

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.