STL, the full English name of sdard template library, can be translated into standard template library or generic library in Chinese. It contains a large number of template classes and template functions. It is a collection of basic templates provided by C + +. It is used to complete functions such as input / output and mathematical calculation. Simply put, STL is a collection of containers, algorithms, and other components.
2、 Composition structure
STL consists of six parts: container, algorithm, iterator, function object, adapter and memory allocator. The last four parts serve the first two parts. Their respective meanings are shown in the figure below.
In the C + + standard, STL is organized into 13 header files.
A container is a class template for storing data. Variable length array, linked list, balanced binary tree and other data structures are implemented as containers in STL. The container can store variables of basic types and objects. When an object or basic type variable is inserted into a container, what is actually inserted is a copy of the object or variable. Containers are divided into two categories: sequential containers and associated containers; It can also be divided into three categories: Sequence container, sorting container and hash container.
1. Sequence container (Sequence container)
Sequence container or sequence container is characterized in that the position of elements in the container is independent of the value of elements, that is, the container will not sort the stored elements. When you insert an element into a container, specify where to insert it (tail, head, or somewhere in the middle) and the element will be located. All sequential containers in STL standard library, including array, vector, deque, list and forward_ list。
2. Associated container (sorting container, ordered associated container)
Association containers are divided into two categories: set and map, and their derivatives multiset and Multimap. The underlying mechanism of these containers is implemented by RB tree (red black tree), which balances one of the binary trees (AVL tree and AA tree). RB tree is also a stand-alone container, but it is not open for use. SGI STL also provides an associative container hash that is not in the standard specification_ Table (hash table), and hash_ Table is a hash completed for the underlying mechanism_ Set hash set, hash_ Map hash mapping table, hash_ Multiset hash, multi key set, hash_ Multimap hash multikey mapping table. Each element in the association container has a key value and a real value. The elements within the associated container are sorted. When inserting an element, the container will put the element in the appropriate position according to a certain sorting rule, so the position cannot be specified when inserting an element. By default, the elements in the association container are sorted from small to large (or by keyword from small to large), and the ` < ` operator is used to compare the size of elements or keywords. Because it is well ordered, the associated container has very good performance in finding.
3. Unordered Association container (hash container)
Unordered associative containers are often referred to as "unordered containers", "hash containers" or "unordered associative containers". Unordered containers are formally introduced into the STL standard library only after the C + + 11 standard. If you want to use such containers, you must choose a compiler that supports the C + + 11 standard. Like associative containers, unordered containers use key value pairs (pair types) to store data. But they are essentially different: the underlying implementation of the relational container adopts the tree storage structure, more specifically the red black tree structure; The underlying implementation of unordered container adopts the storage structure of hash table. When the bottom layer of C + + STL uses hash table to realize unordered container, all data will be stored in a whole continuous memory space, and when the data storage location conflicts, the solution is "chain address method" (also known as "open chain method"). Based on the underlying implementation, different data structures are adopted. Therefore, compared with associated containers, unordered containers have the following two characteristics: ① 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; ② Compared with associative containers, unordered containers are good at finding the corresponding value through the specified key (the average time complexity is O (1)); However, for traversing the elements stored in the container using iterators, the execution efficiency of unordered containers is not as efficient as associative containers. Like associative containers, unordered containers are just a general term for a class of containers, which contains four specific containers, namely unordered_ map、unordered_ multimap、unordered_ Set and unordered_ multiset。 The names of these four unordered containers only add "unordered_" on the basis of the associated container name. Map and unordered_ Take map as an example. In fact, there is only one difference between them, that is, the memory of the map container sorts the stored key value pairs, and unordered_ No. In other words, in the STL of C + + 11 standard, on the basis of providing four kinds of associative containers, their "unordered" versions (unordered version and hash version) are added to improve the efficiency of finding specified elements. In the actual scenario, if a large number of operations of traversing containers are involved, it is recommended to prefer associative containers; Conversely, if more operations are to obtain the corresponding value through the key, the unordered container should be preferred. Note that the hash container was not officially incorporated into the C + + standard library until C + + 11, and before that, "folk" spread hash_ set、hash_ multiset、hash_ map、hash_ Multimap version, but this version can only be used under some compilers that support c + + 11 (such as VS), and some compilers (such as GCC / G + +) do not support it.
4. Container adapter
In addition to the above two types of containers, STL also shields some functions on the basis of the two types of containers, highlights or adds another part of functions, and implements three container adapters: stack, queue and priority queue_ queue。 Container adapter is a class template that encapsulates sequence containers. It provides some different functions on the basis of general sequence containers. It is called an adapter class because it can provide different functions by adapting the existing interfaces of the container. 1. Stack < T >: an adapter class template that encapsulates the deque < T > container. By default, it implements a last in first out (LIFO) push stack. The stack < T > template is defined in the header file stack. 2. Queue < T >: an adapter class template that encapsulates the deque < T > container. By default, it implements a first in first out (LIFO) queue. You can specify a base container for it that meets certain criteria. The queue < T > template is defined in the header file queue. 3. priority_ Queue < T >: it is an adapter class template that encapsulates the vector < T > container. By default, it implements a queue that sorts elements, so as to ensure that the largest element is always in the front of the queue. priority_ The queue < T > template is defined in the header file queue. The adapter class implements some of its own operations based on the basic sequence container. Obviously, it can also add some of its own operations. They provide the advantage of simplifying the common interface and improving the readability of the code.
4、 Member method
All containers have the following two member functions: ① Int size(): returns the number of elements in the container object. ② Bool empty(): judge whether the container object is empty. Sequential and associative containers also have the following member functions: *Begin (): returns an iterator that points to the first element in the container. *End (): returns an iterator pointing to the position after the last element in the container. *Rbegin (): returns a reverse iterator pointing to the last element in the container. *Rend (): returns a reverse iterator pointing to the position before the first element in the container. *Erase (...): Deletes one or more elements from the container. The function parameters are complex and are omitted here. *Clear (): deletes all elements from the container. Note: if a container is empty, the return values of begin () and end () are equal, and the return values of rbegin () and rend () are also equal. Sequential containers also have the following common member functions: *Front (): returns the reference of the first element in the container. *Back (): returns the reference of the last element in the container. * push_ Back (): add a new element at the end of the container. * pop_ Back(): deletes the element at the end of the container. *Insert (...): inserts one or more elements. The function parameters are complex and are omitted here.
The storage methods of the above containers are completely different, so the efficiency of using different containers to complete the same operation is also very different. Therefore, in actual use, we should be good at selecting appropriate containers according to the functions we want to achieve.