[STL] STL containers each common characteristics, STL containers each occasion, STL containers common mechanism, deque, stack, queue

Comparison of the STL deque, stack, queue, list, Set, Map characteristics of each container, using the opportunity to summarize common mechanisms to facilitate the use of containers in the future.
Feel this blog STL classification of each blog will allow you to understand and use almost all of the basics of SLT.

A, STL containers each common characteristics

(1) deque array of double-ended

Operating characteristics: relatively high efficiency at both ends of the insert and delete, insert at the specified location, can also cause the shift data elements, support random access. (With less)
the deque piecewise continuous memory space

(2) stack container

Rules: ;; can not traverse the last out, does not provide an iterator does not support random access.

While(!stack.empty()){
		Cout<<stack.top()<<endl;
		Stack.pop();
}

(3) queue the queue

Rule: FIFO
can not traverse, does not provide an iterator does not support random access

While(!queue。empty()){
		Cout<<queue.frout()<<endl;
		Queue.pop();
}

(4) list a container

STL is very common in two-way linked list
anywhere insertions and deletions efficiency are better, because insertions and deletions relative to the array when it is not necessary to move elements
Disadvantages: does not support random access. Non-contiguous memory space, so that in order to preserve the relationship between predecessor and successor nodes and nodes need to provide additional space overhead
provides its own sort () method, and why? Because they do not support random access, if using an algorithm to improve the level of sort, efficiency will be unstable.
Find () you have to know to find the underlying data type, find the object, providing a callback (condition)

(5) pair of groups

The two values ​​into one value

Pair<string,int>pair1;
Make_pair(“aaa”,1);
Pair3=pair1;

Here Insert Picture Description

Two, STL containers opportunity

(1) Vector usage scenarios:

Such as storage software operation history record, we often want to view the history, such as the last record, the last record, but not to delete records because the records are described facts.

(2) Deque usage scenarios:

Tickets such as queuing systems, memory may be employed to line up the deque, support rapid removal of the head end, quickly add the trailing end. If using Vector, remove the head end, it will move large amounts of data, slow.

Comparison of Vector and the deque

1 "vector.at () () higher than deque.at efficiency, such vector.at (0) it is fixed, but it does start position is not fixed.
2 "If a large release operation, then, vector spend less time with which both internal implementation dependent.
3 "Deque supports rapid insertion and rapid removal of the head, which is an advantage of deque

(3) List of use scenarios:

For example, bus passengers storage, there may be passengers to get off at any time, remove support frequently uncertain position of the element inserted.

(4) Set usage scenarios:

For example sequential storage, storage requirements of the mobile game scoring record individual points in descending order.

(5) Map usage scenarios:

For example, by ID number stored thousands of users, you want to quickly find the corresponding user by ID.
Find the efficiency of a binary tree, then it manifested. If the container is a vector, in the worst case possible to traverse the full container to locate the user.

Three, STL containers common mechanism

STL value are provided by the container (value) meaning, rather than by reference (reference) meaning , that is when we insert element to the container when the inner container copy operation implemented, we will again be a separate element inserted copy a placed in a container, rather than metadata elements directly into the container, that is to say the elements that we offer must be able to be copied.
If the object is not inside a pointer type, then the default configuration, the value of direct copy
if there are objects pointer type, then the need to write their own copy constructor, and reload the equal sign (=)


(1) In addition to the queue and Stack, each container provides the function returns an iterator, using the iterator returned to access the element.
(2) generally do not throw an exception STL, the user needs to pass the correct parameters
(3) each container provides a default constructor and default copy constructor
(4) related to the size of the constructor: 1 "size () returns the number of elements in the container; 2 "empty () determines whether the container is empty.

Published 57 original articles · won praise 28 · views 4123

Guess you like

Origin blog.csdn.net/weixin_41747893/article/details/102959517