C++ Notes - Introduction and Use of the Sixth List

Table of contents

1. Introduction and use of list

1. Introduction to list

2. The use of lists 

2.1 Construction of list

 2.2 Use of list iterators

2.3 list capacity

2.4 list element access

2.5 list modifiers

2.6 The iterator of the list is invalid

Second, the comparison between list and vector




1. Introduction and use of list



1. Introduction to list


1. A list is a sequential container that can be inserted and deleted at any position within a constant range , and the container can be iterated back and forth .


2. The bottom layer of the list is a doubly linked list structure. Each element in the doubly linked list is stored in an independent node that is not related to each other, and the node points to its previous element and the next element through pointers.


3. list is very similar to forward_list : the main difference is that forward_list is a singly linked list , which can only be iterated forward, which makes it simpler and more efficient.


4. Compared with other serial containers (array, vector, deque), list usually has better execution efficiency for inserting and removing elements at any position.


5. Compared with other sequential containers, the biggest defect of list and forward_list is that it does not support random access at any position . For example, to access the sixth element of the list, it must iterate from a known position (such as the head or tail) to this position, iterating over this position requires linear time overhead; the list also requires some additional space to hold the associated information for each node (this may be an important issue for large lists that store elements of smaller types factor )


2. The use of lists 


2.1 Construction of list

Constructor (constructor) interface description
l ist() - construct an empty list


list (size_type n, const value_type& val = value_type()) - the constructed list contains n elements whose value is val


list (const list& x) - copy constructor


list (InputIterator first, InputIterator last) - Constructs a list with elements in the range [first, last)

 2.2 Use of list iterators

Function declaration - interface description
begin + end - returns the iterator of the first element + returns the iterator of the next position of the last element


rbegin +rend——returns the reverse_iterator of the first element, which is the end position, and returns the reverse_iterator of the next position of the last element , which is the begin position

 1. begin and end are forward iterators, perform ++ operation on the iterator, and the iterator moves backward
 2. rbegin(end) and rend(begin) are reverse iterators, perform ++ operation on the iterator, iterate move forward

2.3 list capacity

Function declaration——Interface description
empty——Check whether the list is empty, return true, otherwise return false
size——Return the number of valid nodes in the list 

2.4 list element access

Function declaration - interface description
front - return the reference of the value in the first node of the list
back - return the reference of the value in the last node of the list

2.5 list modifiers

Function declaration——interface description
push_front——insert an element whose value is val before the first element of the list
pop_front——delete the first element in the list
push_back——insert an element whose value is val at the end of the list
pop_back——delete the last element in the list element
insert——insert an element whose value is val in the list position
erase——delete the element at the list position
swap——exchange the elements in the two lists
clear—clear the effective elements in the list

2.6 The iterator of the list is invalid

You can temporarily understand the iterator as similar to a pointer. The invalidation of the iterator means that the node pointed to by the iterator is invalid , that is, the node is deleted. Because the underlying structure of the list is a two-way circular linked list with the leading node, the iterator of the list will not be invalidated when it is inserted into the list. It will only be invalidated when it is deleted, and only the iteration pointing to the deleted node will be invalidated. , other iterators are not affected.

 

 



Second, the comparison between list and vector



The bottom layer of vector is a dynamic sequence table, which is a continuous memory space, so it can support random access, so it is suitable for sorting. However, inserting data will cause capacity expansion, and deleting elements will cause element movement, so it is not suitable for frequent insertion and deletion scenarios.

The memory space of the list is a doubly linked list that can be discontinuous. Because the space is discontinuous, it does not support random access, but its insertion and deletion operations are very efficient, and it is suitable for frequent insertion and deletion scenarios.

Vector will cause iterator invalidation when expanding and deleting elements, while list will cause iterator invalidation when deleting.



 

 (The picture comes from the Internet)

Guess you like

Origin blog.csdn.net/MuqiuWhite/article/details/129690937