Introduction to C + + STL

Source: Internet
Author: User

Introduction of STL

STL (Standard Template Library, Standard template base) is the general designation of a series of software developed by HP Labs. It was developed by Alexander Stepanov, Meng Lee and David R Musser at HP Labs. Now, although it is mainly in C + +, the technology has existed for a long time before it was introduced into C + +.

STL code is broadly divided into three categories: algorithm (algorithm), container (container) and iterator (iterator), almost all of the code in the form of template classes and templates functions, This provides a better opportunity for code reuse than traditional libraries composed of functions and classes. In the C + + standard, the STL is organized into the following 13 header files:<algorithm>, <deque>, <functional>, <iterator>, <vector>, <list>, <map>, <memory>, <numeric>, <queue>, <set>, <stack>, and <utility >. The following author is a brief introduction to the main features of the various parts of the STL.

Second, the algorithm

One common consensus is that the choice of the function library for the data type plays an important role in its reusability. For example, a square-root function that uses a floating-point number as its parameter type is certainly more reusable than using an integer as its parameter class. and C + + through the mechanism of the template allows the postponement of certain types of selection, until the real want to use the template or to the template to be specific, the STL used this point to provide a considerable number of useful algorithms. It completes these algorithms in an effective framework-you can divide all types into a few classes, and then you can replace other types in the same category with one type in the template's parameters.

STL provides a template function of approximately 100 implementation algorithms, such as the algorithm For_each will call the specified function for each element in the specified sequence, stable_sort the sequence with the rules you specify, and so on. Thus, as long as we are familiar with the STL, many code can be greatly reduced, only by calling one or two algorithm templates, you can complete the required functions and greatly improve efficiency.

The algorithm is mainly composed of the main header files <algorithm>,<numeric> and <functional>. <algorithm> is the largest of all STL header files (although it is well understood), it is composed of a large stack of template functions, it can be considered that each function is to a large extent independent, which is commonly used in the range of functions related to the comparison, Exchange, find, traverse operation, copy, modify, removing, reversing, sorting, merging, and so on. The <numeric> is small in size and includes only a few template functions for simple mathematical operations on the sequence, including additions and multiplication on the sequence. In <functional>, some template classes are defined to declare function objects.

Third, the container

In the actual development process, the data structure itself is not inferior to the importance of the algorithm that operates in data structures, when there is a high time requirement in the program, the selection of data structure becomes more important.

The number of classical data structures is limited, but we often repeat some of the code to achieve the vector, linked list, and so on, the code is very similar, but in order to adapt to different data changes in the details of the discrepancy. The STL container provides us with such convenience, it allows us to reuse existing implementations to construct our own specific types of data structures, and by setting some template classes, STL containers provide support for the most commonly used data structures that allow us to specify the data types of elements in a container. Can simplify many of our repetitive and tedious work.

The container is composed primarily of the header files <vector>,<list>,<deque>,<set>,<map>,<stack> and <queue>. For some of the commonly used container and container adapters (which can be considered containers implemented by other containers), you can summarize their correspondence with the corresponding header file in the following table.

Data Describe Implementing header Files
Vectors (vector) Elements that are stored continuously <vector>
Lists (list) A doubly linked list of nodes, each containing an element <list>
Double Queue (deque) An array of continuously stored pointers to different elements <deque>
Collection (SET) A red-black tree of nodes, each containing an element that is arranged between nodes in a predicate that acts on the pairs of elements, and no two different elements can have the same order <set>
Multiple Sets (Multiset) Allows a set of two elements of equal order <set>
Stacks (Stack) The arrangement of the LIFO values <stack>
Queues (queue) Advanced first out of the arrangement of the practice <queue>
Priority Queue (Priority_queue) The order of the elements is a queue that is determined by a predicate that acts on the stored value pair <queue>
Map (map) A set consisting of {key, value} pairs, which are arranged by a function Yu Yu the predicate on the <map>
Multiple mappings (Multimap) Mappings that allow the key pairs to have an equal order <map>

Four, iterators

The following iterator is the most basic part of the function, but it is much more difficult to understand than the previous two (at least I am). Software design has a basic principle, all problems can be introduced by the introduction of an indirect layer to simplify, this simplification in the STL is to use the iterator to do. In a nutshell, an iterator is used in STL to link an algorithm to a container, which acts as a glue and agent. Almost all of the algorithms provided by STL work by using an iterator to access the sequence of elements, each of which defines its own proprietary iterator to access the elements in the container.

The iterator section consists primarily of the header file <utility>,<iterator> and <memory>. <utility> is a very small header file that includes many of the methods used by the iterator in the declaration,<iterator> using several templates in the STL, but it is very difficult to describe the <memory>. It allocates storage space for the elements in the container in an unusual way, and it also provides the main part of the,<memory> for the temporary objects that occur during the execution of some algorithms is the template class allocator, which is responsible for generating the default allocator in all containers.

Some suggestions on learning STL for Beginners

For readers who didn't know much about STL before, the above text is a very general description of the framework of the STL, you understand the STL mechanism and even the use of STL to play a very little help, this is not only because in-depth STL needs to C + + advanced applications have a more comprehensive understanding, More because the STL three part algorithm, the container and the iterator three parts are each other to contain or to say is tightly unifies. Conceptually the most basic part of the iterator, but the direct learning iterator will encounter many abstract and tedious details, but do not really understand the iterator is not directly into the other two parts of the learning (at least for the analysis of the source is this). It can be said that the way to adapt to the STL to deal with the problem is to take a certain amount of time, but at the expense of the STL has achieved a very valuable independence, it through the iterator can be as little as possible to know a data structure of the case to complete the operation of this structure, So determined to delve into the STL friends must not be a momentary difficulty knocked down. In fact, the use of STL model is relatively unified, as long as the adaptation of it, from an STL tool to another tool, there will be no big changes.

For the use of STL, there are two kinds of viewpoints generally. The first is that the most important role of STL is to act as a classical data structure and algorithm teaching material, because its source code involves many specific implementation problems. The second is that the original intention of the STL is to simplify the design, avoid duplication of work, improve programming efficiency, so should be "application first", for the source code is not to delve into. The author believes that the analysis of source code and application is not contradictory, through the analysis of the source code can also improve our understanding of its application, of course, according to the specific purpose can also have different focus.

Finally, the STL is part of the Ansi/iso C + + standard, so for a process that can have a variety of C + + implementations, the first consideration should be the template provided by STL (High efficiency and good portability), The second is the respective libraries of each vendor (efficient but not portable) and write their own code (good portability but inefficient).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.