STL, container related theoretical basis

1. What is STL? What versions are there?

The full name of STL is Standard template library, which is translated into standard template library or generic library, which contains a large number of template classes and template functions, and users can complete functions such as input/output and mathematical calculations.

STL is a collection of containers, algorithms, and other components.

  • HP STL (Hewlett Packard Labs) was defined as an international standard in 1998. It is the father of the STL standard template library. It is the first implementation version of C++ STL. Other versions are generally based on HP STL and are open source.
  • SGI STL, the inherited version of HP STL, is open source, and its source code is readable. Not all compilers that support C++ support this template library in the "folk version", and its performance on the Linux platform is very good.
  • STLport, in order to make SGI STL basic code suitable for VC++, C++ Builder and other compilers, the Russians developed STLport, open source.
  • PJ STL, which can be used by the Visual C++ compiler, is not open source.

2. What is a container? Advantages of containers?

In data storage, there is an object type that can hold other objects or pointers to other objects. This object type is called a container.

  • Containers can hold "other objects" and a set of methods for dealing with "other objects" - container classes are a good solution to certain code reuse problems;
  • Containers can expand by themselves. Unlike arrays, containers do not need to know the number of stored objects in advance, but only need to create objects and reasonably call the provided methods. It will automatically allocate or free memory.

3. Classification of containers

The STL defines three types of generic containers: sequential containers, associative containers, and container adapters.

Sequential container: a linear list of sequential relationships between elements, which is an orderable cluster of linear structures.

Each element has a fixed position unless changed by deletion, insertion, etc.

The relative position is consistent with the logical order. [When the element is inserted into the container, specify the position where the element is, and it will not be automatically sorted according to the size of the element]

E.g:

  • Array<T,N>[Array container: A container provided by C++ itself. Once determined, the length of the container is fixed, which means that elements cannot be added or deleted, and only the value of an element can be changed]
  • Vector<T> [Vector container: a variable-length sequence container, when the storage space is insufficient, it will automatically apply for more memory. Adding or deleting elements at the end is the most efficient, inserting or deleting elements at other positions is less efficient, and the time complexity is O(n)]
  • Deque<T> [Double-ended queue container: very similar to vector, the difference is that using this container is not only efficient to insert and delete elements at the tail, but also efficient to insert or delete elements at the head, and the time complexity is O(1) constant order , but insert or delete an element at a certain position in the container, the time complexity is O(n)]
  • List<T> ["Double" linked list container: is a variable-length sequence consisting of elements of type T, which organizes elements in the form of a doubly linked list, and elements can be efficiently added or deleted anywhere in the sequence ( The time complexity is constant order O(1)), but the speed of accessing any element in the container is slower than the first three containers, because list<T> must be accessed from the first or last element, which requires Move along the linked list until you reach the desired element.
  • Forward_List<T>[Forward linked list container: very similar to the list container, except that it organizes elements in the form of a singly linked list, and its internal elements can only be accessed from the first element, which is faster and more economical than the linked list container memory container]

Associative container: Non-linear structure, there is no strict physical order relationship between elements. A notable feature of associative containers is that they store data in the form of key-values, that is, it can associate keywords and values ​​to store, while sequential containers can only store one type (it can be considered that it only stores keywords, It can also be considered that it only holds the value).

The underlying implementation is a red-black tree. Stored as a linked list, so inserting elements is faster than vector, but searching and appending at the end is slower.

 Container Adapter: C++'s explanation is: an adapter is a mechanism that makes one thing behave like another. A container adapter is a mechanism to make an existing container type work in a way that a different abstract type works.

[PS: The "adapter" in the container adapter is very close to the "adapter" in the common power adapter in life. We know that whether it is a computer, mobile phone or other electrical appliances, 220V AC cannot be used directly when charging. For the convenience of users, each electrical appliance manufacturer will provide a power cord suitable for their own products, which can convert 220V AC Low-voltage direct current for electrical appliances. From the user's point of view, the role of the power cord is to make the otherwise inapplicable alternating current applicable, so it is also called the power adapter.

 4. What type of containers are unordered_set, unordered_map, etc.? What are the characteristics?

Unordered associative containers, which were officially introduced into the STL standard library by the C++11 standard.

The underlying implementation principle is a hash table.

  1. The key-value pairs stored in the unordered container are unordered, and the storage location of each key-value pair depends on the key in the key-value pair.
  2. Compared with associative containers, unordered containers are good at finding the corresponding value by specifying a key (the average time complexity is O(1)); but for using iterators to traverse the elements stored in the container, the execution efficiency of unordered containers is not as good as Associative containers.

 

 [PS: Some readers may ask, since the unordered container is similar to the associative container learned before, which container should be selected in actual use? In general, if a large number of operations involving traversing containers are involved in actual scenarios, it is recommended to prefer associative containers; on the contrary, if more operations are to obtain corresponding values ​​through keys, unordered containers should be preferred]

Reference link: What is C++ STL unordered container (hash container)? (biancheng.net)

Detailed explanation of C++ container

Guess you like

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