C + + vector,list,deque differences

Source: Internet
Author: User
Tags arithmetic

In the writing of C + + program will find that STL is a good thing, reduce the amount of code, so that the code reuse rate greatly improved, reduce the burden of the program ape. There is a container, you will find that if you write a list, queue, or an array, you have to take the time to worry about how to maintain, the inside of the pointer Ah, memory enough to use Ah, length problems, there is no possibility of overflow ah and so on a series of problems waiting for us to solve, or more headache. So the appearance of the container solves this problem, it will be the data structure is encapsulated into a class, only need to add a header file, we can easily apply, not so complicated, even the pointer is encapsulated as an iterator, it is more convenient, more humane, convenient for our programming, for the programmer is still a great gospel!!

The container classes in C + + include "Sequential storage Structure" and "associative storage Structure", which includes vector,list,deque and so on, while the latter includes Set,map,multiset,multimap and so on. If you need to store the number of elements between the compiler can be determined, you can use the array to store, otherwise, you need to use the container class.

1.vector
continuous storage structure, in which each element is contiguous in memorysupports efficient random access and insert/delete operations at the endbut insert/delete operations are inefficient in other locations, equivalent to an array, butthe difference from an arrayFor:expansion of memory space。 Vector support does not specify a vector-sized store, but the expansion of the array requires the programmer to write it himself.
Vector's memory allocation implementation principle:
When STL is implemented internally, first allocates a very large amount of memory space to prepare for storage, that is, the size returned by the capacity () function, When the allocated space is exceeded then the overall reallocation of a piece of memory storage (VS6.0 is twice times, VS2005 is 1.5 times times), so this gives a vector can not specify the vector is the sense of a continuous memory size. Typically, this default memory allocation can do most of the storage.
Expansion space (no matter how large) should be done:
(1) Configure a new space
(2) Move old element one by one to new site
(3) Release the original space back to the system
Note: Vector data is arranged and manipulated in a very similar way to array. The only difference between the two is the flexibility of space utilization. The expansion space of the Array should be written by the programmer himself.
The vector class defines several constructors for defining and initializing vector objects:
Vector<t> v1; The vector holds an object of type T. The default constructor v1 is empty.
Vector<t> v2 (v1); V2 is a copy of v1.
Vector<t> v3 (n, i); The v3 contains n elements with a value of I.
Vector<t> v4 (n); V4 contains n copies of elements that are initialized with values.
2.deque
A continuous storage structure in which each element is also contiguous in memory, similar to a vector, the difference is thatThe deque provides a level two array structure, the first level is exactly like a vector, represents the actual container, and the other level maintains the first address of the container .。 In this way, deque, in addition to having all the functions of a vector,also supports efficient first/end insert/delete operations
Deque dual-ended queue Double-end
Deque is a functionally integrated vector and list.
Advantages: (1) Convenient random access, that is, support [] operator and vector.at ()
(2) Easy to insert and delete inside
(3) Push, pop on both ends
Cons: Memory-intensive
3.list
non-contiguous storage structurehas a doubly linked list structure, each element maintains a pair of forward and back pointers, thus supporting forward/back traversal。 Efficient random Insert/delete operations are supported, but random access is inefficient and the overhead is higher because of the need for additional maintenance pointers. Each node includes a message fast info, a precursor pointer pre, and a rear-drive pointer post. You can easily add and delete operations without allocating the necessary memory size. Use a non-contiguous memory space for storage.
Advantages: (1) Do not use continuous memory to complete dynamic operation.
(2) Easy to insert and delete inside
(3) Push, pop on both ends
Cons: (1) No internal random access is allowed, i.e. [] operator and vector.at () are not supported
(2) memory-intensive relative to Verctor
Usage differences:
(1) If you need efficient immediate access, instead of the efficiency of INSERT and delete, use vector
(2) If you need a large number of insertions and deletions and do not care about random access, you should use the list
(3) If you need random access, and care about the insertion and deletion of both ends of the data, you should use Deque
4.vector vs. List vs. Deque:
A, if you need random access operation, select vector;
b, if you already know the number of elements to be stored, select vector;
C, if you need to insert/delete randomly (not just at both ends), select the list
D, only need to be in the first end of the insertion/deletion operation, but also to take into account the efficiency of random access, only select Deque, otherwise all select vector.
E, if the need for random insertion/deletion, but also need random access, you need to make a compromise between the vector and list-deque.
F, when you want to store a large responsible class object, the list is better than the vector, of course, you can also use vectors to store pointers to objects,
It is also highly efficient, but the maintenance of pointers is very error-prone and therefore not recommended.

question one: The difference between list and vector:
(1) Vector allocates a contiguous address space for the stored object, and the random access efficiency is very high. Butinserts and deletes need to move a lot of data, less efficient。 Especially when vectors are stored
object is large, or the constructor is complex, the copy constructor is executed when the existing element is copied.
(2) List ofobject is discrete, random access needs to traverse the entire list, access efficiency is lower than vector。 But inserting elements into the list, especially at the end of the end, is very efficient and requires only changing the pointer of the element.

(3)vector is unidirectional, and list is bidirectional ;

(4) The iterator in the vector is released after use, but the list of the linked list is different, and its iterator can continue to use after it is used; the list is unique;

Question two: The difference between deque and vectors:

(1) Both sides can quickly insert and delete elements . vectors can only be carried at the tail end .

(2) deque element access and iterator operations are slightly slower. Because the internal structure of deque will be one more indirect process.

(3) Iterators are special smart pointers, not generic pointers. It needs to jump between different chunks.

(4) because Deque uses more than one piece of memory (and the vector must use a contiguous memory), Deque's max_size () may be larger .

(5) control over the timing of capacity and memory allocation is not supported.

Note: Inserting and deleting elements in addition to the ends will cause any pointers, references, iterators that point to the deque element tofail. However,deque memory redistribution is better than vector. Because its internal structure shows no need to copy all elements.

6,deque memory block is no longer used, it will be released. the memory size of the deque can be reduced. However, it is not done and what is done is defined by the implementation version.

Q Topic Three: Constant container const
     const vector<int> VEC (10);// The capacity and size and values in this container are immutable, and the const modifier is vector;
      iterators: const vector<int>::const_iterrator Ite Const iterator,
        NOTE: const vector <int> VEC (10)--with const int A[10] is one thing, meaning Vec has only 10 elements, Cannot be added, the elements inside are also not changeable

 vector<int  > A (10  );  const  vector<int  > B (10  ) a[ 1 ]=10 ; //  correct  b[1 ]=10 ; //  error  a.resize (20 ); //  correct  b.resize (20 ); //  error  

Question four: capacity v.s size
A,capacity is the total number of elements that can be filled before the container needs to grow, and only containers with continuous storage have capacity concepts (such as vector,deque,string), and list does not need to be capacity.
b, size is the number of elements currently stored by the container.
c, vector default capacity initial value, and growth rules are dependent on the compiler.
Question five: When storing a custom class object with a vector, the custom class object must satisfy:
A, there is a parameterless constructor available for invocation (default or custom);

b, there are available copy assignment functions (default or Custom)

Question six: iterator iterator

A, vector, and deque iterators support arithmetic operations, and the list's iterators can only perform ++/--operations and do not support ordinary arithmetic operations.

B. The iterator in the vector is released after use, but the list of lists is different, and its iterators can continue to be used after use; the list is unique;

C + + vector,list,deque differences (RPM)

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.