The container classes in C + + include "Sequential storage Structure" and "associative storage Structure", which includes vector,list,deque and so on, while the latter includes Set,map,multiset,multimap and so on. If you need to store the number of elements between the compiler can be determined, you can use the array to store, otherwise, you need to use the container class.
1 , Vector
A continuous storage structure in which each element is contiguous in memory;
Support efficient random access and Insert/delete operations at the end, but insert/delete operations at other locations are inefficient;
2 , Deque
The continuous storage structure, where each element is also contiguous in memory, is similar to a vector, except that Deque provides a two-level array structure, with the first level being exactly like a vector, representing the actual container, and the first address of the container being maintained at the other level.
In this way, the deque supports efficient first-end insert/delete operations in addition to all the functions of the vector.
3 , List
A discontinuous storage structure with a doubly-linked list structure, with each element maintaining a pair of forward and back pointers, thus supporting forward/back traversal.
Efficient Random Insert/delete operations are supported, but random access is inefficient and the overhead is higher because of the need for additional maintenance pointers.
4 , Vector v.s. List v.s. deque:
A, if you need random access operation, select vector;
b, if you already know the number of elements to be stored, select vector;
C, if you need to insert/delete randomly (not just at both ends), select the list
D, only need to be in the first end of the insertion/deletion operation, select Deque, otherwise select vector.
E, if you need to randomly insert/delete, but also need random access, you need to make a compromise between the vector and the list.
F, when you want to store a large responsible class object, the list is better than the vector, of course, the vector can also be used to store pointers to the object, but also to achieve high efficiency, but the maintenance of pointers is very error-prone, so it is not recommended.
5 , capacity v.s size
A, capacity is the total number of elements that can be filled before the container needs to grow, and only containers with continuous storage have capacity concepts (such as vector,deque,string), and list does not need to be capacity.
b, size is the number of elements currently stored by the container.
c, vector default capacity initial value, and growth rules are dependent on the compiler.
6 , when storing a custom class object with a vector, the custom class object must satisfy:
A, there is a parameterless constructor available for invocation (default or custom);
b, there are available copy assignment functions (default or Custom)
7 , iterator iterator
A, vector, and deque iterators support arithmetic operations, and the list's iterators can only perform ++/--operations and do not support ordinary arithmetic operations.
The following is an overview of the entire list:
Standard Container class |
Description |
Sequential containers |
Vector |
Quick Insert and delete from behind, direct access to any element |
Deque |
Quick Insert and delete from front or back, direct access to any element |
List |
Double linked list, quick insert and delete from anywhere |
Associative containers |
Set |
Quick Find, no duplicate values allowed |
Multiset |
Quick Find, allow duplicate values |
Map |
One-to-many mappings, fast lookup based on keywords, no duplicate values allowed |
Multimap |
One-to-many mappings, quick find based on keywords, allow duplicate values |
Container Adapter |
Stack |
Last in, first out |
Queue |
Advanced First Out |
Priority_queue |
The highest priority element is always the first out-of-order |
Common functions for all standard libraries
Default constructor |
Provides a constructor for container default initialization. |
Copy constructor |
constructor that initializes the container to an existing copy of the same container |
Destructors |
Destructors for memory grooming when containers are no longer needed |
Empty |
Returns true if there are no elements in the container, otherwise false |
Max_size |
Returns the maximum number of elements in a container |
Size |
Returns the current number of elements in a container |
Operator= |
Assign a container to another container |
operator< |
Returns true if the first container is less than the second container, otherwise returns false, |
operator<= |
Returns true if the first container is less than or equal to the second container, otherwise false |
Operator> |
Returns true if the first container is greater than the second container, otherwise false |
operator>= |
Returns true if the first container is greater than or equal to the second container, otherwise false |
operator== |
Returns true if the first container equals the second container, otherwise false |
operator!= |
Returns true if the first container is not equal to the second container, otherwise false |
Swap |
Swapping elements of two containers |
None of the operator>,operator>=,operator<,operator<=,operator==,operator!= are suitable for priority_queue
Sequential container and associative container common functions
Begin |
Two versions of the function return iterator or const_iterator, referencing the first element of the container |
End |
The function of two versions returns iterator or Const_iterator, referencing the last element of the container after a |
Rbegin |
Two versions of the function return Reverse_iterator or const_reverse_iterator, referencing the last element of the container |
Rend |
The function of two versions returns reverse_iterator or Const_reverse_iterator, referencing the first element of the container before a |
Erase |
Clear one or several elements from a container |
Clear |
Clears all elements in the container |
The following table shows the commonly used typedef in the order container and the associated container, which are common to general declarations of variables, parameters, and function return values.
Value_type |
Types of elements stored in containers |
Reference |
A reference to a container that holds an element type |
Const_reference |
A constant reference to the element type in the container that can read only the elements in the container and perform a const operation |
Pointer |
A pointer to a container that holds the type of element |
Iterator |
An iterator that points to the type of element in the container |
Const_iterator |
A constant iterator that points to the type of element in the container and can read only the elements in the container |
Reverse_iterator |
A reverse iterator that points to the type of element in the container, which iterates backwards in the container |
Const_reverse_iterator |
A reverse iterator that points to the type of element in the container and can read only the elements in the container |
Difference_type |
The type of the result of the two iterators that reference the same container (list and associative containers do not have a operator-defined) |
Size_type |
The type used to calculate the number of items in the container and the Retrieval order container (cannot be retrieved for list) |
8. Sequence class container
(1) vector vectors are equivalent to an array
Allocate a contiguous amount of memory space in memory for storage. Supports storage that does not specify vector size. STL internal implementation, first allocates a very large amount of memory space to prepare for storage, that is, the size of the capacity () function returned, when more than this allocated space, and then the overall reallocation of a memory storage, which gives the vector can not specify the vector is the sense of a continuous memory size. Typically, this default memory allocation can do most of the storage.
Pros: (1) do not specify contiguous storage of an array of memory size, which can be manipulated like an array, but can be dynamically manipulated. Usually manifested in push_back () Pop_back ()
(2) Convenient random access, that is, support [] operator and vector.at ()
(3) Save space.
Cons: (1) Internal Insert delete operation is inefficient.
(2) Push and pop can only be carried at the end of the vector, not on the head of the vector.
(3) When the dynamically added data exceeds the vector's default allocated size, the overall redistribution, copying and release
(2) List doubly linked list
Each node includes a message fast info, a precursor pointer pre, and a rear-drive pointer post. You can easily add and delete operations without allocating the necessary memory size. Use a non-contiguous memory space for storage.
Advantages: (1) Do not use continuous memory to complete dynamic operation.
(2) Easy to insert and delete inside
(3) Push, pop on both ends
Cons: (1) No internal random access is allowed, i.e. [] operator and vector.at () are not supported
(2) memory-intensive relative to Verctor
(3) deque double-ended queuing double-end queue
Deque is a functionally integrated vector and list.
Advantages: (1) Convenient random access, that is, support [] operator and vector.at ()
(2) Easy to insert and delete inside
(3) Push, pop on both ends
Cons: (1) More memory usage
Usage differences:
1) If you need efficient and immediate access, rather than the efficiency of insert and delete, use vector
2) If you need a large number of insertions and deletions and do not care about random access, you should use the list
3) If you need random access and are concerned with inserting and deleting data at both ends , you should use Deque
The container class in C + + is detailed