The difference between vector and list of

You talk about the difference between vector and list the application, the more detailed the better?

1, concepts:

1)Vector

Continuous storage container, dynamic array, the space allocated on the heap

The underlying implementation: Array

Twice the capacity growth:

When the vector increases (insert) a new element, if not exceed the capacity of the time, then there is space left, then added directly to the last (insert the specified location), then adjust the iterator.

If there is no remaining space, the space will be reconfigured twice the original number of elements, elements of the space and the original space by way of initializing a new copy, the new element to increase the space again, and finally destructor releases the original space before iterator will fail.

performance:

Visit: O (1)

Insert: insert at the end (enough space): Soon

In the final insertion (not enough space): the need for memory allocation and release, as well as a copy of the data before.

In the intervening (enough space): the memory copy

In the intervening (enough space): the need to apply and release the memory, and the data prior to copying.

Delete: Delete the last: soon

In the middle of Delete: Memory copy

Applicable scene: frequent random access, and do not often non-tail node insertion and deletion.

2) List

Dynamic list, allocated on the heap space, each insert a metadata will be allocated space, each element will be deleted to free up space.

Bottom: a doubly linked list

performance:

Access: random access performance is poor, only to quickly access the beginning and end nodes.

Insert: Soon, generally constant overhead

Delete: Soon, generally constant overhead

Applicable scene: often insert delete large amounts of data

2, the difference between:

1) vector underlying implementation is an array; list is a doubly linked list.

2) vector supports random access, list is not supported.

3) vector is a sequence memory, list not.

4) vector for insertion and deletion in the intermediate node causes memory copy, list not.

5) vector-time allocation of memory is good, not only 2-fold expansion; list each time you insert a new node will be memory applications.

6) vector of random access performance, poor performance insertion and deletion; poor list of random access performance, insertion and deletion performance.

3. Application

vector has a contiguous memory space, support random access, if the need for efficient access immediately, but not with the insertion and deletion of efficiency, the use of vector.

list does not have a period of continuous memory space, if you need an efficient insertions and deletions, not on random access, you should use the list.

Guess you like

Origin www.cnblogs.com/kandid/p/11369057.html