Http:dev.9cbs.netdevelopArticle1616310.SHTM C ++ STL Introduction

xiaoxiao2021-03-06  16

1. STL Introduction STL (Standard Template Library, Standard Template Library) is a collective design of a series of software developed by 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 the C standard, STL is organized as 13 headers below:

,

,

,

,

,

,

,

,

,

,

,

with

. The following author briefly introduces the main features of each part of the STL. Second, a consensus that everyone can achieve is that the function 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 from the head file

,

with

composition.

It is the biggest one in all STL header files (although it is very understandable), it is composed of a large stack of mode functions, which can be considered to be independent, in which the function ranges commonly used To compare, exchange, look, traverse operation, copy, modification, removal, reverse, sort, merge, etc.

The size is small, including only a few template functions that perform simple mathematics operations above the sequence, including some of the additions and multiplication in the sequence.

Some template classes are defined to declare function objects. Third, the container is in the actual development process, the importance of the data structure itself will not be infromated to the importance of the algorithm for operation in the data structure, and when there is a portion of the data in the program, the selection of the data structure appears 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 part is mainly by the header file

,

,

,

,

,

with

composition. For commonly used containers and container adapters (which can be considered as containers implemented by other containers), the correspondence between them and the corresponding header files can be summarized by the following table. Data Structure Description Implement Head File Vector (Vector) Continuously Stored Elements

The list (list) is a two-way linked list consisting of nodes, each node contains an array of elements that continuously store pointers to different elements.

Collection (SET) is a red-black tree consisting of nodes, each node contains an element, and a node is arranged in some kind of predicate, no two different elements can have the same order.

MultiSet allows for a collection of two sequence equal elements

Stack (stack) rearrangement

Queue Advanced Unit

The order of the priority queue (priority_queue) element is a queue determined by some predicate that acts on the stored value

Mapping (MAP) is arranged in a collection of {keys, value}, arranged in a predicate that acts on the booker

MultiMap allows the key to map with equal order. The iterator is the most basic part of the iterator, but it is much more effluent than the top two (at least the author is like this. ). 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 by the header file

,

with

composition.

It is a small header file that includes a statement that runs through several templates used in STL,

Many methods used in iterators are provided, and for

The description is very difficult, it assigns storage space in different forms of elements in the container, and also provides mechanisms that are generated during certain algorithm execution.

转载请注明原文地址:https://www.9cbs.com/read-45658.html

New Post(0)