Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the difference between vector and list in C++

Shulou Source: shulou.com Published: 2022-06-02 03:55:25 09月14日 Update

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)

Tags: Support access data memory space error compilation clocking code data structure variable learning selection example content dynamic operator efficiency array Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Shulou Technology NVidia vpn Xiaomi