[Reserved] C ++ STL Overview

Source: https://www.cnblogs.com/dyllove98/p/3214898.html

What is a container

First, we must understand what is container, which is defined in C ++ as follows: on the data storage, there is a type of object that can hold other objects or pointers to other images of this object type is called a container. Very simple, container objects that hold other objects, of course, this is a simple to understand, this "object" also includes a series of processing "other objects" approach, because these methods are often used in the design of the program , the container also reflects the advantage that "container class is a good solution for a particular problem reuse of code."

Yet another feature is the container vessel may extend themselves. In solving problems we often do not know how many we need object storage, which means that we do not know how much memory space should be created to hold our object. Obviously, arrays are powerless in this regard. The advantages of container here, it does not need to tell you in advance how many objects you want to store it, as long as you create a container object, and it provides a reasonable method calls, handle all the details by the container itself is complete. It can apply to your memory or free up memory, and use the best algorithm to execute your command.

With the birth of the container is object-oriented language and proposed container class is especially important in an object-oriented language, even it is considered to be the foundation of early object-oriented language. Now almost all object-oriented languages are also accompanied by a set of containers, in C ++, is the Standard Template Library (STL).

And different other languages, C ++ processing vessel is a template-based approach. Standard C ++ library container provides a variety of data structures, these data structures can work well with a standard algorithm , which provides good support for our software development!

General classification of container

STL generic container defined for three types: sequential containers, and containers associated with the container adapter.

Sequential vessel  is between one kind of elements of a linear relationship between the order table, the structure may be a linear sequence cluster. Sequential container each element has a fixed position unless this location change delete or insert operation. The position of the element itself and independent of time and place, and the operations related to the order but the container is not directly saved logical element sequence when sorting operation according to the characteristics of the element. For example, we order a disposable container for three additional elements, the logical order of these three elements relative position and the additional container are consistent.

Associative containers  and the order is not the same container, the container is non-linear associative tree structure, a more accurate to say that a binary tree structure. No strict sequential relationship between the physical elements, i.e. elements not logical sequence element when stored in a container into the container. However, associated containers offer another sort function according to the characteristics of the elements, so that the iterator can be obtained according to the characteristics of the element "sequentially" element.

Notable feature is its associative containers is another key way to save data, which means it can associate keywords and values ​​saved, and the order can only preserve a container (can be considered only save keywords can also be considered only saves the value). This particular class of container below illustrates this point.

Container adapter  is a relatively abstract concept, C ++ explanation is: the behavior of the adapter is a thing of a mechanism similar to the behavior of other things. Container adapter is to allow the type of container for existing uses a different type of abstract works to implement a mechanism. In fact, only the interface conversion occurred. So you can understand it as a container vessel, it is the essence of a container, but he does not rely on specific types of standard containers, it is understood that the template container. Or understand it as the interface of the container, and which specific type of container using an adapter to achieve, you can decide when you define the adapter.

The following table lists three concrete container class definition STL container contains:

Standard containers

Feature

Sequential container

vector

Rapid insertion and deletion from behind, direct access to any element

and

From the front or the back of rapid insertion and deletion, direct access to any element

list

Doubly linked list, quickly insert and remove it from anywhere

Associative containers

set

Quickly find, does not allow duplicate values

multiset

Quick Search allows duplicate values

map

Many mapping, based on keywords to quickly find, does not allow duplicate values

multimap

Many mapping, based on keywords to quickly find, allow duplicate values

Container adapter

stack

LIFO

queue

FIFO

priority_queue

Highest priority element is always the first out of the column

Sequential container

Vector vector:  

It is a linear sequence structure. It corresponds to the array, but its size may not be specified in advance, and automatically extended. It can be operated like an array, due to its characteristics we can be seen as a dynamic array vector.
After creating a Vector, it is automatically assigned a memory in contiguous memory space for data storage, the initial space size can be specified in advance may be specified by default Vector, i.e., the size of the capacity () function return value. When the data storage space than the allocated vector will reallocate a block of memory, but such allocation is very time-consuming, when re-allocation of space it would do this kind of action:

First, vector will apply for a larger memory block;

Then, the original copy of the data to the new memory block;

Secondly, in destroying the original memory block objects (calling the object's destructor);

Finally, the original memory space freed.

If the amount of data stored vector is large, such an operation would lead to poor performance (which is relatively easy vector is designed to copy the value of the reason type). So the vector is not under what circumstances the performance is good, only in the performance of vector known in advance in case its size is optimal.

{Attached. How to explain the size of the vector when it was declared.

Source: https://www.runoob.com/w3cnote/cpp-vector-container-analysis.html

};

vector features:
(1) specified as an array of the same continuous storage , but can be dynamically extended space. I.e. it can operate like an array, and may be dynamic operation. Usually reflected in the push_back () pop_back ().
(2)  a random access convenience, it is like an array is accessed, i.e. support [] operator and vector.at ()
(. 3) to save space, because it is continuously stored in the data storage area is not wasted, but make it clear that in most cases is not the full vector stored in the storage area is actually not wasted.

(4)  insertion within the delete operation efficiency is very low, such an operation is substantially prohibited . Vector is designed to only append, and delete operations on the back end, the reason is the internal vector is achieved in accordance with the principles of the order table.
(5) can only push and pop in the vector of last, can not push and pop in the vector head.
(6) When the vector data is dynamically added exceeds the default size allocated to reallocate, and release the copy memory, this operation is very consumption performance. So to vector achieve optimum performance, it is best to specify the size of the space when you create vector.

-------------------------------------------------------------------------------------------

Doubly linked list list

Is a linear chain structure, which consists of a plurality of data nodes, each node comprising a block of information (i.e., the data is actually stored), and a pointer to a rear drive precursor pointer. It need not allocate the specified memory size and may be any stretch, because it is stored in non-contiguous memory space, and ordered by the pointer element linked.

Due to its structure, list of random retrieval performance is very bad , because it does not directly address as vector elements to find, but to find a re-order one, so the target element on the list, it's time to retrieve longer. Search time is proportional to the position of the target element.

Although random retrieval fast enough, but it can quickly insert and delete operations at any node. Because each node list of preserved its position in the linked list, insert or delete elements of a maximum of only three elements have an impact on the storage address all elements of the vector would be operational after the point has not affected this vector that is unparalleled.

list of features:
(1) without the use of contiguous memory space which can be freely dynamic operation;
(2) can be quickly inserted or deleted at any position inside, of course, may be performed at both the push and pop.
(3)  can not access the interior of the random , i.e., not supported [] operator and vector.at ();
(. 4) with respect to verctor consume more memory.

------------------------------------------------------------------------

Deque deque 
is an optimized, both ends of the container base sequence elements sequences add and delete operations. It allows more rapid random access, but it is not all the objects vector stored in a contiguous block of memory, instead of using a plurality of contiguous memory blocks, and save the trace of the blocks and their sequence in a mapping structure . Add or delete elements to the ends of the deque little overhead. It does not require re-allocation of space, the end of the element is increased to be more effective than vector.

In fact, the deque is a combination of vector and list the advantages and disadvantages , it is in a container therebetween.

deque features:
(1) easy random access, i.e. to support [] operator and vector.at (), but no good performance Vector;
(2) insertions and deletions can be internal, but the performance is less than List;
(. 3) may be performed at both ends of push, pop;

--------------------------------------------------------------------

Comparison of the three

A vector is a contiguous block of memory, the deque is a plurality of contiguous memory blocks, list all the data elements stored separately, may be any two elements are not consecutive.

Preferably the vector query performance, and also very good at the end of the data increases, unless it is re-application memory segment; for efficient random access memory.

list is a linked list, any element may be discontinuous, but it has two elements and a pointer to the next element a. So insert, delete elements of the performance is the best, and query performance is very poor; for a large number of insertions and deletions do not care about the needs of random access.

deque is somewhere in between, and that both the advantages of an array list, which is a combined block and a plurality of linked list arrays. So it has been a good list query performance, there is a good vector insertion, deletion performance. If you need random access and care to insert and delete data at both ends, then deque is the best choice.

Associative containers

set, multiset, map, multimap tree structure is a nonlinear, specifically a more efficient use of the special balanced binary tree retrieval - red-black tree.

Because these four classes associated container vessels use the same principle, so they are the core of the algorithm is the same, but they have some differences in the application, to describe the differences between them.

SET, also called set is actually a collection of elements, but the value of elements contained therein is unique and is arranged in a certain order, the set of each element in the set of instances is referred to. Because it is internal to the organization by way linked list, so faster than the vector at the time of insertion, but was slow in finding and vector added at the end. When you insert the same key and value is the value of the current element.

{

"In fact, set a special case of the map , although there is no set form for this key element, but the set itself is the key value, key-value pairs was mapped on the map where the element itself can be seen as the mapping itself. so, both on implementation should be a very big coincidence . and conceptually, set can be achieved by the map, thus becoming a container adapter, but there is no reason to do that, I think the greatest extent to save memory of it, save the value of value is completely unnecessary. "

Source: https://www.cnblogs.com/lgxZJ/p/6349084.html

}

multiset, is a collection of multiple, its implementation and the set is similar, except that it does not require the elements in the collection is unique, meaning that the same element can appear multiple times in the set.

map, providing a - relationship "key value" one to one data storage capacity. Its "key" can not be repeated in the container, and arranged in a certain order (in fact, we can also be set as a key - the value stored relationship, but it is not only the key value which is a special form of the map.) . Because it is based on the list of stored, it also inherits the strengths and weaknesses of the list.

the multimap, and principles substantially similar map, which allows "button" in the container may not be unique.

Characteristics associated with the container are obvious, relative to the order container, have the following characteristics:

1, which is realized inside the nonlinear binary tree structure, specifically to the principle of red-black tree structure implemented;

2, set and map elements to ensure uniqueness, mulset mulmap extended and this property may allow for elements not unique;

3, elements are ordered set, in ascending order by default when inserted.

Based on the above characteristics,

1,  the associated element of the container insert and delete operations faster than vector , since the vector sequence is stored, and the association is linked storage container; than list slower , because even if they are the same chain structure, but a linear list , the binary tree is associated with the container, an element which changes relate to changes other than to List element, and it is sorted, each insertion and deletion need reordering element;

2,  the associated container retrieving operation of vector elements than slower, but much faster than list . vector sequence is stored contiguously, of course, it can not match, but relatively chained list is much faster because the list one by one search, search is proportional to the time it with the size of the container, the container and find the associated complexity of basic is Log (N), such as if there are 1000 records, to find a maximum of 10 times, 1,000,000 records, to find a maximum of 20 times. The larger the container, the container relative to the associated list to reflect the more superior;

3, in the use of set different from the vector, the most important feature deque, list is a set of internal sorting, which though inferior to the query vector, but much stronger than list.

4, using the map function is not substituted, which holds the relationship between the data "key", and this embodiment uses a relationship class key array. Arrays are digital type index to the index position of the element, and the map is used to index the elements of the key character position. In use the map also provides a way to operate the array type, i.e. it can be retrieved through the index data, which is the other containers can not, of course, including set. (STL only vector, deque, map elements can be operated by means of an array of classes, i.e. as ele [1] mode)

-------------------------------------------------------------------------

Container adapter

STL consisting of three adapters: Stack stack, queue, and queue priority priority_queue.

Adapter is the interface of the container, which itself can not save elements directly, the mechanism that holds the element is to call another container order to achieve that the adapter can be thought of as "save a container, the container then save all the elements."

STL three adapters provided to achieve a container of some sort of order. Default queue stack and container based deque implemented, priority_queue implemented based on the vector container . Of course, in creating an adapters may also be specified to achieve a particular container, container in order to specify a specific parameter on the second adapter to create the adapter can override the default implementation.

Because of the characteristics of the adapter, the adapter is not a sequence may be composed of any of a container can be achieved.

Characteristics of the stack is LIFO stack, so that its associated base container may be any container sequence, since these types of container structures may be provided with a stack operation request, which are provided push_back, pop_back and back operation;

Queue FIFO queue is characterized, that the base container adapter requires association must provide pop_front operation, so it can not be established on the vector container;

Priority queue priority_queue adapter random access request function, so the list can not be established in the container.

--------------------------------------------------------------------------

{

"Some of the differences on the order of container and associated container briefly summarized as follows: 

(1) The essential difference associated container (Associative Container) the order container (Sequential Container) is characterized by: the container is associated by a key (key) storing and reading elements, is sequentially stored sequentially by the container position in the container element and access elements.
(2) in STL, where the "order" and "associated" refers to the upper interface access method manifested, not the underlying storage. Why divide it? Because users of the STL, they do not need to know the underlying container implementation mechanism, as long as know how to access the container through the upper interface elements on it, otherwise contrary to the generic containers originally designed.
(3) STL as the underlying structure to achieve a storage container mainly vectors, linked lists, binary trees, and combinations thereof. And vector sequence using the container main chain as a basic memory structure, and combinations thereof, such as the various stacks and queues, and associated containers balanced binary search tree is stored as the underlying structure.
"

Source: https: //blog.csdn.net/JIEJINQUANIL/article/details/51175858}

Guess you like

Origin www.cnblogs.com/jiading/p/11108731.html