In STL, the basic containers include: string, vector, list, deque, set, map set, and map are unordered storage elements, you can only access the set: set through the interface provided by the set: set to determine whether an element is in a group. It uses less map: ing, which is equivalent to a dictionary, map a value to another value. If you want to create a dictionary, use string, vector, list, deque, and set as an ordered container. stringstring 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 performance is the fastest; if it is for the middle or start part of the operation, such as adding or deleting elements there, or replace the element. Memory replication is required 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 will have pre-reserved memory, if you use it in large quantities, there will be a waste of memory, which needs to be considered. in addition, when deleting an element, you should not release too much memory. 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 = "abcd Efg "; string y (s); // when the memory is re-allocated, y will only allocate memory larger than the content in s, so the waste is not very large. 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 with a string of capacity returned values, there are other return values after the operation 2. vectorvector 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. This is only required if 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. to sum up the need for frequent random access, use vector 3. 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, it can be seen that the performance of adding and deleting elements to a list is very high, and the memory is not needed to be moved. Of course, every element does not need to be constructed or parsed, therefore, it is often used to perform random operations on containers. however, when accessing the elements in the list, the first and last access to the fastest access to other elements is O (n), so if you need frequent random access, 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 lot of memory 4. the deque dual-end queue stores the content in the heap. it is saved as follows: [heap 1] www.2cto.com... [heap 2]... [heap 3] Each heap saves 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 at the beginning, there is also a high random access speed. vector can quickly add and delete elements at the end, and can quickly access any element list. It can quickly add and delete elements in all places, however, you can only quickly access the first and last element deque as quickly as adding elements at the beginning and end, and provide a random access method, like a vector, using [] to access any element, however, the random access speed is not as fast as that of vector, because it requires internal processing of heap redirection. 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.