First, STL introduction
STL (Standard Template Library, Standard Template Library) is a collective name of a series of software developed in HP Lab. It is developed by Alexander Stepanov, Meng Lee, and David R Musser working in HP laboratory. Although it is now mainly in C , the technology has existed for a long time before being introduced into C .
STL's code is divided into three categories: Algorithm (algorithm), container (container), and iTerator (iterator), almost all code uses template classes and template functions, which is compared to traditional The library of functions and classes is a better code reuse opportunity. In C standards, STL is organized to include the following 13 headers: ,
Second, algorithm
One consensus that everyone can achieve is that the library has a crucial role in the selection of data types. For example, a function of a square root, is definitely a highly use integer as its parameter type when using floating point numbers as its parameter type. The mechanism of C through the template allows for postponed some types of options until the STL provides considerable useful algorithms for the Template or when the template is turned. It is in a valid framework to complete these algorithms - you can divide all types into a few categories, then you can use a type in the template parameter to replace other types in the same class.
STL provides a template function for approximately 100 implementation algorithms, such as the algorithm for_each to call each element in the specified sequence to call the specified function, Stable_sort, stabilize the sequence with the rules you specify, and the like. In this way, as long as we are familiar with STL, many code can be greatly limited, only need to call one or two algorithm templates and greatly enhance efficiency.
The algorithm part is mainly composed of header file
Third, container
During the actual development process, the importance of the data structure itself will not be inferior to the importance of the algorithm of the data structure, and when there is a part of the program high, the selection of the data structure is more important.
The classic data is limited, but we often repeat some code written in order to implement the structure of the vector, linked list, which is very similar, but in order to adapt to different data changes. The STL container provides us with convenience, which allows us to repeat the data structure of the existing implementation of the implementation of their specific types, by setting some template classes, STL containers provide support for the most common data structure, these templates The parameter allows us to specify the data type of the elements in the container, you can simplify our many repetitions of work. The container portion is mainly composed of header file ,
Data Structure Description Implement Head File Vector (VECTOR) Continuously Stored Elements Double queue (Deque) continuous storage pointing to different elements The array of pointers is composed of
Fourth, iterator
The iterator to say is the most basic part of the object, but it is difficult to understand that it is more laborious than the top (at least the author). Software design has a basic principle, all issues can be simplified by introducing an indirect layer, which is simplified in STL to use iterators. In summary, the iterator is used in STL to link algorithms and containers to a viscous agent. Almost all algorithms provided by STL are operated by the iterator access element sequence, each container defines the iterator that itself is used to access the elements in the container.
The iterator part is mainly composed of header file
5. Suggestions for beginners learn STL
For readers who haven't learned STL before, the above text is just a generally described STL's framework, which is slightly slightly slightly helping you understand the STL mechanism and even STL, which is not only because of deep STL There is a comprehensive understanding of the advanced application of C , more than three partial algorithms of STL, and the container and iterator three parts are homogeneous or closely combined. From the conceptual part of the concept, it is an iterator, but the direct learning iterator will encounter a lot of abstract boring and cumbersome details, but do not really understand the iterator is unable to go directly to the other two parts of learning (at least the source code) It is like this. It can be said that the method of adapting to the STL processing problem is to take a certain amount of time, but at the expense of this, STL has achieved a very valuable independence, which can know some data structure as little as possible through iterators. In the case of the operation of this structure, the friends who determined that STL to be drilled, don't be knocked down by the difficulties. In fact, the model of STL is relatively unified, as long as it adapts to it, there will be no big changes from a STL tool to another tool. There are also two views on STL use. The first one considered that the maximum role of STL is to act as a classic data structure and algorithm textbook because its source code involves many specific implementations. The second one believes that the original intention of STL is to simplify design, avoid duplication of labor, improve programming efficiency, so it should be "applied first", and do not have to be more important for source code. The author believes that the analysis source code and application are not contradictory. It can improve our understanding of the application by analyzing the source code, and of course, according to the specific purpose, there may be different focuses.
The last thing to say is that STL is part of the ANSI / ISO C standard, so for a process that can have a variety of C implementation, first considering the template (efficient and portable) of STL (efficient and portable), followed by each The corresponding libraries of the manufacturer (efficient but portable are not good) and write code (good portability but low efficiency).