A preliminary study of C + + STL container

Source: Internet
Author: User

What is a container

First, we have to understand what a container is, in C + +, where a container is defined as having an object type on the Datastore, which can hold other objects or pointers to other pairs of objects, which are called containers. Very simply, a container is an object that holds other objects, which, of course, is a simple understanding, and this "object" also contains a series of methods to deal with "other objects", because these methods are often used in the design of the program, so the container also embodies a benefit, that is, " Container classes are a good solution to the problem of reusing specific code. "

Another feature of the container is that the container can expand itself. We often do not know how many objects we need to store in order to solve the problem, that is to say we do not know how much memory space should be created to hold our objects. Obviously, the array is also powerless in this respect. The advantage of the container is here, it does not require you to tell it beforehand how many objects you want to store, as long as you create a container object, and reasonably call the method it provides, all the processing details will be done by the container itself. It can either request memory for you or free up memory, and execute your commands with an optimal algorithm.

Containers are proposed with the advent of object-oriented languages, and container classes are particularly important in object-oriented languages, even though they are considered to be the foundation of early object-oriented languages. In today's almost all object-oriented languages, there is also a container set, in C + +, the standard Template Library (STL).

Unlike other languages, handling containers in C + + is a template-based approach. The container in the standard C + + library provides a variety of data structures that work well with standard algorithms, which provides good support for our software development!

Classification of general purpose containers

STL has three types of generic containers defined: sequential containers, associative containers, and container adapters .

A sequential container is a linear table with sequential relationships between elements , and is a linear structure of a sequential cluster. Each element in a sequential container has a fixed position, unless the position is changed by a delete or insert operation. This position is not related to the element itself, but to the time and place of the operation, the sequential container does not sort by the characteristics of the element but directly preserves the logical order of the element operation. For example, we add three elements to a sequential container at once, and the relative position of the three elements in the container is consistent with the logical order of the Append.

The associative container is not the same as the sequential container, and the associative container is a non-linear tree structure, and more accurately, it is a two-fork tree structure . There is no strict physical order between the elements, which means that the elements do not have a logical order in the container when the elements are placed into the container. But associative containers provide another feature that is ordered based on the characteristics of the elements, so that iterators can "sequentially" acquire elements based on their characteristics.

Another notable feature of associative containers is that it holds data in the form of a key value, which means that it can be used to correlate keywords and values, whereas sequential containers can only hold one (it can be thought that it only holds the keyword, or it can be considered to hold only the value). This can be illustrated in the following specific container class.

The container adapter is a relatively abstract concept, and the C + + explanation is that an adapter is a mechanism for making one thing behave like another . A container adapter is a mechanism that enables an existing container type to work in a different type of abstraction . in fact, only interface transitions have occurred . Then you can understand it as a container container, which is actually a container, but he does not depend on the specific standard container type, it can be understood that the container template. Or it is understood as the interface of the container, and the adapter is specific to which type of container to implement, when defining the adapter can be determined by you .

The following table lists the specific container classes contained in the three types of containers defined by STL:

Standard Container class

Characteristics

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

Vector, deque and list

sequential containers : vector vectors :

is a linear sequential structure. Equivalent to an array, but its size can be unspecified and automatically expanded. It can be manipulated like an array, and because of its nature we can view vectors as dynamic arrays.
After creating a vector, it automatically allocates a contiguous memory space in memory for data storage, the initial space size can be specified beforehand or by the vector by default, this size is the return value of the capacity () function. The vector allocates a chunk of memory when the stored data exceeds the allocated space, but the allocation is time-consuming and it does this when reallocating space:

First, the vector will apply for a larger chunk of memory;

Then, copy the original data into the new memory block;

Second, destroy the object in the original memory block (call the destructor of the object);

Finally, the original memory space is freed.

If the amount of data the vector is holding is large, such an operation will certainly result in poor performance (which is why the vector is designed to be a more easily copied value type). So vector is not under what circumstances performance is good, only in advance to know its size, the performance of the vector is optimal.

Features of Vector:
(1) specifies a block of contiguous storage as an array, but the space can be dynamically extended. That is, it can operate like an array, and it can be manipulated dynamically. Usually manifested in push_back () Pop_back ().
(2) Random access, it is accessed like an array, that is, support for the [] operator and vector.at ()
(3) Save space, because it is continuous storage, in the area of data storage is not wasted, but to make it clear that a little vector is not full, in most cases is not stored in the area is actually wasted.

(4) In the internal insert, delete operation is very inefficient, such operations are basically forbidden. Vectors are designed to append and delete operations on the backend only because the implementation of the vector is based on the principle of the sequential table.
(5) Push and pop can only be carried at the end of the vector, not on the head of the vector.
(6) When the dynamically added data exceeds the size assigned by the vector, the memory is redistributed, copied and released, which consumes performance. Therefore, to achieve the optimal performance of vectors, it is best to specify the space size when the vector is created.

doubly linked list List

is a linear linked list structure whose data consists of several nodes, each of which consists of a block of information (that is, the actual data stored), a precursor pointer, and a back-drive pointer. It does not need to allocate the specified memory size and can scale arbitrarily, because it is stored in a non-contiguous memory space and is linked by a pointer to an ordered element.

Because of its structural reasons, the list random retrieval performance is very bad, because it does not directly find the address of the element as a vector, but to find the order from the beginning to the other, so that the more the target element, its retrieval time is longer. The retrieval time is proportional to the position of the target element.

Although the speed of random retrieval is not fast enough, it can be quickly inserted and deleted at any node. Because each node of the list holds its position in the linked list, inserting or deleting an element affects only a maximum of three elements, unlike vectors, which have no effect on the storage address of all elements after the operation point.

Features of list:
(1) Do not use continuous memory space so that can be arbitrarily dynamic operation;
(2) can be quickly inserted or deleted anywhere inside, of course, you can also push and pop at both ends.
(3) No internal random access is allowed, i.e. [] operator and vector.at () are not supported;
(4) Use more memory relative to Verctor.

double-ended queue deque

is an optimized basic sequence container for adding and deleting elements on both ends of a sequence. It allows for faster random access, but it does not keep all objects in a contiguous block of memory like a vector, but instead uses multiple contiguous blocks of storage and keeps track of those blocks and their order in a mapping structure. Adding or removing elements to deque is a small cost. It does not need to reallocate space, so adding elements to the end is more efficient than vector.

In fact, Deque is a combination of the pros and cons of vectors and lists, which is a container between the two.

Features of Deque:
(1) Convenient random access, that is, support [] operator and vector.at (), but the performance of the vector is not good;
(2) can be inserted and deleted internally, but the performance is not the list;
(3) can be push, pop at both ends;

comparison of the three

Describes the features of the vector, list, and deque in the memory structure:

A vector is a contiguous block of memory, and Deque is a contiguous block of memory, and the list is stored separately for all data elements, which can be any two elements without a continuous.

The query performance of the vector is the best, and it is good to add data at the end, unless it re-applies to the memory segment and is suitable for efficient random storage.

A list is a linked list, and any element can be discontinuous, but it has two pointers to the previous and next elements. So it is the best performance for inserting and deleting elements, and the query performance is very poor; it is suitable for a large number of insert and delete operations without concern for random access requirements.

Deque is in between, and it takes into account the advantages of arrays and linked lists, which are the union of a chunked list and multiple arrays. So it has a good query performance by the list, has been good for the insertion of vectors, delete performance. Deque is the best choice if you need to immediately access and care about both data insertions and deletions.

Associative containers

Set, Multiset, map, Multimap is a non-linear tree structure, in particular, it is a kind of relatively efficient and special balanced retrieval binary tree-red-black tree structure . (As for what is the red and black tree, I do not understand, can only understand that it is a binary tree structure)

Because these four container classes of the associative container all use the same principle, their core algorithms are consistent, but they have some differences in the application, first describe the differences between them.

set, also known as a set, is actually a collection of elements , but the values of the elements contained therein are unique and are arranged in a certain order, and each element in the collection is referred to as an instance of the collection . Because the interior is organized in a linked list, it is faster to insert than vector, but is slower to find and add to the end.

Multiset , which is a multiset, is implemented in a similar way to set, except that it does not require that the elements in the collection be unique, that is, the same element in the collection can appear multiple times .

map, which is a one-to data storage capability for a "key-value" relationship . Its "keys" are not duplicated in the container and are arranged in a certain order (in fact, we can also think of the set as a key-value relationship store, except that it has no value for the key.) It is a special form of map). Because it is stored as a linked list, it also inherits the advantages and disadvantages of the list.

Multimap, which is basically similar to the principle of map, allows "keys" to be not unique in a container .

The characteristics of associative containers are obvious, compared with the sequential containers, there are several main features:

1, its internal implementation is a non-linear two-fork tree structure, in particular, the structure of the red and black trees to achieve the principle;

2, set and map ensure the uniqueness of the element, Mulset and Mulmap extend this attribute, can allow the element is not unique;

3, the element is an ordered set, by default when inserted in ascending order.

Based on the above characteristics,

1, associative containers are quicker to insert and delete elements than vectors because vectors are stored sequentially, and associative containers are chained, and are slower than lists because even though they are chained, the list is linear and the associative container is a two-tree structure. The change of an element involves more changes in other elements than a list, and it is sorted, and each insertion and deletion requires reordering of elements;

2, associative containers are slower to retrieve elements than vectors, but much faster than lists. Vector is sequential storage, of course, but the relative chain of the list is much faster because the list is a search, its search time is proportional to the size of the container, and the correlation container lookup is basically the complexity of log (N), for example, if there are 1000 records, up to 10 times, 1,000,000 records, found up to 20 times. The larger the container, the better the correlation container is relative to the list;

3, in the use of the set difference from the Vector,deque,list is the largest feature is set is the internal ordering, which in the query, although inferior to the vector, but greatly stronger than the list.

4, the function of using map is irreplaceable, it holds the "key-value" relationship of the data, and this key-value relationship takes the way of class array. An array is an index of a numeric type that indexes the position of an element, and map is the position of the element using a character keyword. The use of map also provides a way to manipulate an array of classes, that is, it can be indexed to retrieve data, which other containers do not, of course, including set. (Only vectors and maps in the STL can manipulate elements by way of an array of classes, i.e. ele[1]

Container Adapter

The STL contains three types of adapters: stack stack, queue queues, and priority Priority_queue .

The adapter is the interface of the container, which itself cannot save the element directly, its mechanism to save the element is to invoke another sequential container to implement, that is, the adapter can be thought of as "it holds a container, this container to save all the elements."

The three types of adapters available in the STL can be implemented by a single sequential container. The default stack and queue are based on the Deque container implementation, and the Priority_queue is based on the vector container. Of course, you can also specify a specific implementation container when creating an adapter, and specifying a specific sequential container on the second parameter to overwrite the default implementation of the adapter when creating the adapter.

Because of the characteristics of the adapter, an adapter can not be implemented by any sequential container.

Stack stack is characterized by LIFO, so its associated basic container can be any one of the sequential containers, because these container type structure can provide the operation of the stack, they all provide push_back, pop_back and back operations;

Queue queues are characterized by FIFO, which requires that the underlying container to which the adapter is associated must provide pop_front operations, so it cannot be built on a vector container;

The priority queue Priority_queue adapter requires random access, so it cannot be built on the list container.


A preliminary study of C + + STL container

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.