Stl:standard template library (standard template library or generic library) is a component of c++ standard library. STL realizes the common data structure and algorithm once with the help of template, and separates the data structure and algorithm. STL has been completely built into the compiler supporting c++ without additional installation, which may be one of the reasons why STL is widely used. STL is provided in the form of source code. Basically speaking, STL is a collection of containers (template classes encapsulated with data structures), algorithms and other components, which basically achieves a high degree of optimization of various storage methods and related algorithms.
Alexandar Stepanov (the father of STL Standard Template Library) cooperated with Meng Lee. Is open source.
An inherited version of HP STL, which is open source and readable
In order to make the basic code of SGI STL applicable to various compilers such as vc++ and c++ builder, open source
It is implemented by referring to HP STL and is also an inherited version of HP STL. Therefore, this header file not only contains the relevant authorization information of HP STL, but also the copyright information of p.j.plauger. Non open source.
Rouge Wave STL
Developed by Rouge wave, it is also a version of HP STL. It is not open source.
2、 Basic composition of STL
STL is composed of container, algorithm, iterator, function object, adapter and memory allocator. Iterator, function object, adapter and memory allocator serve container and algorithm.
|container||Some template classes that encapsulate data structures|
|algorithm||STL provides a large number (about 100) of data structure algorithms, which are designed as template functions. These algorithms are defined in the STD namespace. Most of them are contained in the header file <algorithm> and a few in the header file <numeric>|
|iterator||The reading and writing of the data in the container is completed by the iterator, which acts as the glue between the container and the algorithm|
|Function object||A class overloads the () operator as a member function. This class is called a function object class. The object of this class is a function object (also known as a functor)|
|Adapter||The interface (template parameters) of a class can be adapted to the form specified by the user, so that two classes that cannot work together can work together. It is worth mentioning that containers, iterators, and functions all have adapters|
|Memory allocator||Provide customized memory application and release functions for container class templates|
In the c++ standard, STL is reorganized as13Header file, as follows:
It may be for backward compatibility or internal organization planning. Some STL versions store two files with and without extensions at the same time. It is recommended to follow the c++ specification,Use header file without extension。
3、 What is the container
In the actual development process, the way of accessing data will directly affect the complexity and time consumption of adding, deleting, modifying and querying them.
Simply understand the container, that isA collection of some template classes, but different from ordinary template classes, the container encapsulates methods (that is, data structures) for organizing data。 STL providesClass 3 standard vessel, including sequence container, sorting container and hash container. The latter two types of containers are sometimes collectively referred to as associated containers, as shown in the following table:
|Sequence container||It mainly includes vector container, list container and deque double ended queue container. It is called a sequence container because the position of elements in the container is independent of the value of elements, that is, the container is not sorted. When you insert an element into a container, you specify where the element will be located.|
|Sort container||It includes set collection container, multiset multi collection container, map mapping container and multi map multi mapping container. By default, the elements in the sorting container are sorted from small to large. Even if the element is inserted, it will be inserted into the appropriate position. Therefore, the correlation container has very good performance in searching.|
|Hash container||C++ 11 adds four new associative containers, namely unordered_ Set hash set, unordered_ Multiset hash multiset, unordered_ Map hash map and unordered_ Multimap hash Multimap. Unlike the sorting container, the elements in the hash container are not sorted, and the position of the elements is determined by the hash function.|
The storage methods of the above three types of containers are completely different, so the efficiency of using different containers to complete the same operation is also very different. Therefore, in actual use, the most suitable container should be selected according to specific functions.
Whether it is a sequence container or an association container, the most common operation is undoubtedly to traverse the elements stored in the container. In most cases, the “iterator” will be used to implement this operation.
Although the internal structures of different containers are different, they are essentially used to store a large amount of data. Therefore, some operation methods of containers are similar. Therefore, generic technology can be used to design a general algorithm applicable to all containers, never separating containers from algorithms.
1. Iterator class
The STL standard library defines an iterator type for each standard container, that is, the iterators of different containers are different, so the iterator function of containers is also different, which determines whether containers support some algorithms in STL.
Commonly used iterators are divided intoInput iterator, output iterator, forward iterator, bidirectional iterator, random access iterator5. Input iterators and output iterators are special. They do not treat arrays or containers as operands, but input / output streams as operands. I won’t talk about input iterators and output iterators here for the time being.
Suppose there is an iterator ITER
1) Forward iterator
ITER supports ++iter, iter++, *iter operations, copied or assigned values, and can use = = and= Operator for comparison
2) Bidirectional iterator
Bidirectional iterators have all the functions of forward iterators. In addition, they can also perform –iter or iter– operations (that is, move backward one position at a time).
3) Random access iterator
Random access iterators have the full functionality of bidirectional iterators. In addition, assuming I is an integer variable or constant, ITER also supports the following operations:
|Iter+=i||Make ITER move back I elements|
|Iter-=i||Make ITER move forward I elements|
|Iter+i||Returns the iterator of the ith element after ITER|
|Iter-i||Returns the iterator of the ith element before ITER|
|Iter[i]||Return the reference of the ith element after ITER|
The two random access iterators P1 and P2 can also be compared with the <, >, < =, > = operators. In addition, the expression p2-p1 is also defined, and its return value represents the difference between the sequence number of the element pointed to by P2 and the element pointed to by P1.
5、 Iterator corresponding to container
|array||random access iterators|
|vector||random access iterators|
|deque||random access iterators|
|set / multiset||Bidirectional Iterator|
|map / multimap||Bidirectional Iterator|
|unordered_map / unordered_multimap||forward iterators|
|unordered_set / unordered_multiset||forward iterators|
|stack||Iterators are not supported|
|queue||Iterators are not supported|
The container adapters stack and queue do not have iterators. They contain member functions that can be used to access elements.
2. How iterators are defined
|Forward iterator||Container class name:: iterator iterator name;|
|Constant forward iterator||Container class name:: const_ Iterator iterator name;|
|reverse iterator||Container class name:: reverse_ Iterator iterator name;|
|Constant inverse iterator||Container class name:: const_ reverse_ Iterator iterator name;|
*Iterator Name: the element pointed to by the iterator
The difference between constant iterators and non constant iterators is that non constant iterators can also modify the elements they point to
The difference between a reverse iterator and a forward iterator is that when performing a++ operation on a forward iterator, the iterator will point to the latter element in the container; When performing a++ operation on a reverse iterator, the iterator points to the previous element in the container.
The above four ways of defining iterators are not applicable to every container.