Efficiency Comparison of STL containers

Source: Internet
Author: User

1. Introduction to sequential storage containers:string, vector, list, deque associated storage container: Map the bottom of the tree structure, most of the use of balanced binary tree implementation, find a value is a constant time, the effect is good, but each time the insertion value, will re-form the bottom of the balanced binary tree, the efficiency has a certain impact.Setand 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.2.string    stringIt's basic_string<.CharThe implementation of > is continuously stored in memory. In order to improve efficiency, there will be reserved memory, such as String s="ABCD", the space used by S may be 255, and the string will not allocate memory again when it adds content to S. The memory is not requested again until the content is >255, thus improving its performance. When the content >255, string allocates a new memory first. Then copy the content and copy the previous content. (copy-on-Write technology) The operation of the string, if it is added to the last, generally do not need to allocate memory, so the performance is the fastest; If you are in the middle or start a partial operation, such as adding elements there or deleting elements, or instead of 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, so it can be more efficient the next time it is used. Because string has pre-reserved memory, there is a waste of memory if it is used in large quantities, which needs to be considered. There is also the need to remove an element without releasing too much memory, which should also be considered. The memory in string is allocated in the heap, so the length of the string can be very large, and char[] is allocated in the stack, 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 normal char[] instead of using string. String is used in cases where the length of the string is unknown or varies greatly. If the string has gone through several additions and deletions, now the size is much smaller than the maximum size, and you want to reduce the size of the string used:strings ="ABCDEFG";stringY (s);//Because the memory is allocated again, Y will only allocate memory larger than the content in S, so the waste will not be very largeS.swap (y);//reduce the memory used by SIf you have enough memory, you don't have to think about it. Capacity is a function that looks at the memory currently in use. You can try string to assign a string after the capacity return value, as well as the return value after other operations3vector vectors are dynamic arrays. It is also allocated memory in the heap, the elements are stored continuously, there is reserved memory, and the memory is not released if the size is reduced. If the new value>The memory will be allocated at the current size. The vector expands capacity by twice times each time it expands, which is very efficient for small objects.        The fastest operation for the last element (the fastest to add at a later time), usually does not require the movement of memory, only need to keep enough memory.        Adding a Delete element to the middle and start operation requires moving the memory, and if your element is a struct or a class, it will also be constructed and refactored at the same time, so the performance is not high (it is better to put the pointer of the struct or class in the vector instead of the struct or class itself, which avoids the construction and destruction of the move). Access, access to any element is O (1), which is constant, so vectors are often used to store content that needs to be randomly accessed frequently, and there is no need to frequently add delete operations to intermediate elements. Compare the vector's properties to the same string, You can also use capacity to look at the current reserved memory, using swap to reduce the memory it uses.4The list list is the linked list, and the elements are stored in the heap, and each element is placed in a single piece of memory. List has no space to reserve the habit, so each allocation of an element will be allocated from memory, each deletion of an element will release its occupied memory, which is different from the above, But watch out. list where to add the delete element performance is very high, do not need to move memory, of course, there is no need to construct and deconstruct each element, it is often used to do the random operation of the container. But accessing the elements inside the list is the fastest access to the other elements, which are O (n). So if you need random access, use other good5The . Deque double-ended queue, which 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 are pointers between the heap and the heap, which looks like a combination of list and vector, but it is also true deque allows you to quickly add deleted elements in front of you, or to quickly add deleted elements in the back, Then there can also be a higher random access speed vector which is able to quickly add the deleted element at the end and can quickly access any element. List is a quick way to add deleted elements everywhere, However, only the first and last elements can be accessed quickly. Deque at the beginning and last add elements are the same fast, and provides a random access method, like a vector using [] access to any element, but random access speed than vector fast, Because it wants to handle heap jumps internally. Deque also has reserved space. In addition, since the deque does not require continuous space, it is important to note that the elements that can be saved are larger than the vectors. There is also the need to move elements in front and back without moving other blocks, so the performance is high6. Vector vs List vs Dequevector: The number of objects changes small, the object is simple, the random access element is frequent, but does not care about the efficiency of inserting and deleting list: The number of objects varies greatly, objects are complex, insertions and deletions are frequent, And don't care about the deque: need to be accessed immediately, and concerned about the insertion and deletion of both ends of the data list< pointer > is completely the least-performing approach, in which case the vector< pointer > good, because the pointer is not constructed and destructors, and does not occupy much memory
http://blog.csdn.net/luojinping/article/details/7799395

Efficiency Comparison of STL containers

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.