STL provides three basic containers: vector, list, And deque.
The vector and built-in arrays are similar. They have a continuous memory space and the starting address remains unchanged.
It supports instant access, that is, the [] Operator. However, because its memory space is continuous
Insertion and deletion may result in a copy of the memory block. In addition, when the memory space after the array is insufficient, you need to re-
Apply for a large enough memory and copy the memory. These greatly affect the efficiency of vector.
List is a two-way linked list in the data structure (according to SGI STL Source code ), So its memory space can be discontinuous
, Through the pointer to access data, this feature makes its immediate access becomes very inefficient, so it
The [] OPERATOR overload is not provided. However, due to the characteristics of the linked list, it supports deletion of any location with good efficiency.
And insert.
Deque is a double-ended queue. Its implementation is unclear, but it has the following two features:
It supports [] operators, that is, instant access, and is less efficient than vector. It supports
Operation: push_back, push_front, pop_back, pop_front, and so on, and the efficiency of list operations on both ends
It's similar.
Therefore, in actual use, how to select one of the three containers should be determined according to your needs, generally follow the following
Principles:
1. If you need efficient instant access without worrying about the efficiency of insertion and deletion, use the 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.
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.