The STL provides three sequential containers: vector, List, and deque. Vectors and deque are all implemented using arrays, and lists are implemented using linked lists. On the implementation of the three principles and the respective application of the scene we do a bit of analysis.
1.vector is a dynamic array, which allocates memory in the heap and allocates memory if the number of elements in the vector is greater than the current size. It has a contiguous memory space, and the starting address is the same, so it is well supported for random access elements, the [] operator. The vector operates at the end of the element (adding or deleting elements) as quickly as it does without moving memory. But if you insert or delete an element in the middle, it will be inefficient because you want to move the memory.
2.deque is a container that supports the efficient insertion of data into both ends, and the element data is stored in a block-based linear structure. The elements in the container are segmented in a fixed array of sizes, and the container needs to maintain an indexed array that holds the first address of these arrays. As a result, inserts are high at both ends of the deque, but it is still very inefficient to insert and delete them internally.
3.list is implemented as a linked list for applications that require frequent insert and delete operations in containers.
On the overhead side, the vector has the least overhead, deque slightly higher, and the list is the highest.
Common functions in a sequential container:
Assign (N,elem): Adding n copies of the formulated elements to the container
Assign (Beg,end): An element that assigns a value from an iterator beg to end
Push_back (Elem): Attaching an element to a container
Pop_back (): Delete container tail element
Front (): Returns the first element of the container
Back (): Returns the container tail element
Insert (Position,elem): Inserts an element into the container at the specified location
Vector-specific functions:
Vector (n,element): Constructs a vector that fills in n copies of the specified elements
Vector (Beg,end): Constructs a vector that is initialized with an iterator beg the element between end.
Vector (size): Constructs a vector of a specified size
At (index): DataType returns the element at the specified position
Deque-specific functions:
Deque (n,element): Constructs a double-ended queue, filling in n copies of a given element
Deque (Beg,end): Constructs a double-ended queue that is initialized with the element between the iterator beg to end
Deque (size): Constructs a double-ended queue of a specified size
At (index): DataType returns the element at the specified position
Push_front (Element): Inserting elements into the team header
Pop_front (): Delete team first element
List-specific functions:
List (n,element): Constructs a list that fills in n copies of a given element
List (Beg,end): Constructs a list that is initialized with an iterator beg to an element between end
List (size): Constructs a list of a specified size
Push_front (Element): Inserting an element into a list header
Pop_front (): Delete list header element
Remove (Element): Removes all elements in the list that are equal to the specified element
Splice (pos1,list2,beg,end): After this function is called, pos1 becomes empty until the element from beg to end in List2 is moved to this list list2
Sort (): Sort the list in ascending order
Merge (LIST2): Assuming this list and list2 are sorted, merge list2 into this list, List2 empty
Reverse (): Reverse-order this list
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"STL Container Learning Notes"-sequential containers