Introduction to C++ Containers

In object-oriented programs, the concept of containers is mostly introduced. So what is a container? In essence, it is a collection of objects of the same type, but it is not just as simple as an array, it implements a more complex data structure than an array and can achieve more complex functions. The C++ standard template library provides 10 general-purpose containers, which can basically solve most of the problems encountered in the program.

what is a container

The definition of a container in C++ is as follows: On data storage, there is an object type that can hold other objects or pointers to other objects. This object type is called a container. In layman's terms, a container is an object that holds other objects. This "object" also contains a number of methods for dealing with other objects, which also reflects one of the benefits of the container class, "a good solution to the problem of specific code reuse by container classes. ".

Another advantage of containers is that they can be expanded by themselves. The problem is that we don't know how many objects we need to store. Arrays are lacking in this regard. The container can allocate memory for you, free it, and use the optimal algorithm to execute your commands.

Classification of Generic Containers

(1) Sequential container: A linear table with sequential relationship between elements is an ordered cluster of linear relationship. The elements in the sequential container have a fixed position, unless deletion and insertion are used to change its position. This position has nothing to do with the element itself, but with the time and location of the operation.

vector: quickly delete and insert from the back, accessing any element;

deque: Quickly delete and insert from the front or back, accessing any element;

list: Double entry, insert and delete from any position;

(2) Associative container: non-linear tree structure (binary tree structure), there is no strict physical order between elements. The associative container provides the function of sorting according to the characteristics of the elements, and the iterator takes the elements according to the "order" of the characteristics of the elements.

set: fast search, no duplicate values ​​are allowed;

multiset: fast lookup, allows duplicate values;

map: One-to-many mapping, fast search based on keywords, no duplicate values ​​allowed;

multimap: one-to-many mapping, fast search based on keywords, allowing duplicate values;

(3) Container Adapter: A mechanism that allows an existing container type to work in the way of a different abstract type. Essentially only interface transitions take place.

stack: last in first out;

queue: first in first out;

priority_queue: The highest priority is the first to dequeue;

sequential container

(1) Vector vector: We can regard it as a dynamic array. After creating a vector, it will allocate a continuous area in memory to store data. The initial test space can be specified or determined by a vector (the return value of the capacity() function) . When the data storage space exceeds the allocated space, the vector will reallocate a memory area. Such allocation is time-consuming. The actions are as follows:

First, the vector applies for a larger memory area;

Then, copy the original data to the new memory area;

Second, destroy the original memory block data;

Finally, free the original memory space.

So, if the vector holds a large amount of data, such an operation can lead to poor performance. The performance of vector is the best only if the space size is known in advance.

(2) Doubly linked list List: Doubly linked list structure, pointers link all elements. According to its structural characteristics, the retrieval performance of List is not good, it needs to be searched sequentially, and the retrieval time is proportional to the position of the target element. But it works for fast deletes and inserts.

(3) Double-ended queue deque: Containers inserted and deleted at both ends of the sequence can be quickly and randomly searched. Unlike vector, which stores all elements in the same memory block, it uses multiple consecutive memory blocks, and a map structure keeps track of the order of these blocks and elements. A deque is a combination of the advantages and disadvantages of vector and List, and it is a container in between. Support [] and vector.at (), the performance is not as good as vector.

associative container

set, multiset, map, and multimap are all tree structures with non-linear structure, using efficient balanced retrieval binary tree - red-black tree structure.

(1) set: a set of elements, the element values ​​are unique and arranged in a certain order, each element in the set is called an instance of the set, and the interior is organized in a linked list structure;

(2) multiset: multiple sets, the implementation is similar to set, and the element values ​​are not unique;

(3) map: Provides a one-to-one data storage capability of "key-value". "Keys" cannot be repeated in the container and are arranged in a certain order;

(4) multimap: The principle is the same as above, and the "key" can be repeated in the container.

Insertion and deletion of associative containers are faster than vector and slower than List, and elements need to be reordered after each insertion and deletion;

Associative container retrieval is slower than vector, but much faster than List;

container adapter

The three adapters provided by STL can be implemented by a certain sequence container. The default stack and queue are implemented based on the deque container, and priority_queue is implemented based on the vector.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326398518&siteId=291194637