What is the difference between vector and list in C++
This article mainly explains "what is the difference between vector and list in C++". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the difference between vector and list in C++".
A table allows you to understand the difference between vector and list vectorlist data structure: sequential table (dynamic array) ring two-way linked list physical space: continuous discontinuous memory consumption: less or more variable size: variable-dynamic array variable-random storage by pointer: support (vector overloaded []) does not support (because discontinuous) advantage: support random storage efficient insertion of any node, Operational disadvantages such as deletion: inserting and deleting to copy memory does not support random access
Comparison of data structures:
So how do we choose to use vector and list?
When choosing which data structure to use, you should follow this principle:
1. If you only care about random access and don't care about inserts and deletions, then consider vector
two。 If you only care about insertion and deletion, not random access, then consider list
Examples
When dealing with a large amount of data, the data is relatively fixed, do not care about sorting and intermediate value insertion and deletion, but select vector when querying and filtering.
When the order of the data is changed, the data changes, for example, the ranking list needs to be transposed, inserted and deleted, and select list.
Because of these differences between list and vector, there are some differences between list::iterator and vector::iterator. Take a look at the following example:
# include # include # include using namespace std; int main (void) {vector v; list l; for (int iTuno; I)