What is STL?

Source: Internet
Author: User

1. Introduction to STL

STL (Standard Template Library) is a collection of software developed by the HP lab. It was developed by Alexander Stepanov, Meng Lee, and David R musser while working at the HP lab. Although it mainly appears in C ++, this technology has existed for a long time before it was introduced into C ++.

STLCodeIn a broad sense, there are three types: algorithm (Algorithm), Container (container) and iterator (iterator), almost all the Code uses the template class and template function method, this provides a better chance of code reuse than a traditional library composed of functions and classes. In the C ++ standard, STL is organized into the following 13 header files: <algorithm>, <deque>, <functional>, <iterator>, <vector>, <list>, <map>, <memory>, <numeric>, <queue>, <set>, <stack>, and <utility>. Below I will briefly introduce the main features of each part of STL.

Ii. Algorithms

One consensus that everyone can achieve is that the function library plays a crucial role in selecting data types for reusability. For example, the reusability of a square root function when floating-point numbers are used as its parameter type is definitely higher than the parameter class when integer is used as its parameter type. C ++ allows you to postpone the selection of Certain types through the template mechanism until you really want to use the template or make the template special, STL uses this to provide many useful algorithms. It completes these algorithms in an effective framework-you can divide all types into a few categories, then, you can use one type in the template parameter to replace other types in the same type.

STL provides about 100 template functions to implement algorithms. For example, the for_each algorithm calls the specified function for each element in the specified sequence, stable_sort uses the rules you specify to sort the sequence in a stable manner. In this way, as long as we are familiar with STL, many codes can be greatly simplified. By calling one or two algorithm templates, we can complete the required functions and greatly improve the efficiency.

The algorithm consists of the header file <algorithm>, <numeric>, and <functional>. <Algorithm> is the largest of all STL header files (though well understood). It is composed of a large number of template functions. It can be considered that each function is independent to a large extent, the commonly used functions include comparison, exchange, search, traversal, replication, modification, removal, reversal, sorting, and merging. <Numeric> it is very small. It only includes several template functions that perform simple mathematical operations on the sequence, including addition and multiplication operations on the sequence. <Functional> defines some template classes to declare function objects.

Iii. Container

In the actual development process, the importance of the data structure itself is not inferior to the importance of algorithms that operate on the data structure. WhenProgramWhen there is a part with high requirements on time, it is more important to choose a data structure.

The number of classic data structures is limited, but we often repeat some code written to implement vectors, linked lists, and other structures. These codes are very similar, it is just to adapt to the changes of different data and make some difference in details. The STL container provides us with this convenience. It allows us to reuse existing implementations to construct our own data structures of specific types. By setting some template classes, STL containers provide support for the most common data structures. The parameters of these templates allow us to specify the Data Types of elements in the container, which can simplify a lot of repetitive and tedious work.

The container consists of header files <vector>, <list>, <deque>, <set>, <map>, <stack>, and <queue>. For some commonly used containers and container adapters (which can be seen as containers implemented by other containers), you can summarize their relations with the corresponding header files through the following table.

Data Structure Description implementation header file

Elements continuously stored by vector <vector>

A list is a two-way linked list composed of nodes. Each node contains an element. <list>

An array consisting of pointers to different elements continuously stored in the deque queue <deque>

A set is a red-black tree composed of nodes. Each node contains an element, which is arranged by a certain predicate acting on the element pair, no two different elements can have the same order <set>

Multiple sets (Multiset) allow two sets of elements in the same order <set>

Arrange the values of the stack (stack) before, before, and after <stack>

Queue first-in-first-out orders <queue>

The order of priority_queue elements is determined by a certain predicate acting on the stored value pair. <queue>

Map is a set of {key, value} pairs that act on the predicates of a key pair. <map>

Multimap allows key pairs to be mapped in equal order. <map>

Iv. iterator

The iterator mentioned below is the most basic part in terms of its role, but it is more effort-consuming than the previous two (at least I do ). There is a basic principle in software design. All problems can be simplified by introducing an indirect layer, which is completed by the iterator in STL. In summary, the iterator is used in STL to associate the algorithm with the container and acts as a viscosity and agent. Almost all algorithms provided by STL work through the iterator to access the element sequence. Each container defines its own proprietary iterator to access the elements in the container.

The iterator consists of the header file <utility>, <iterator>, and <memory>. <Utility> is a small header file that includes declarations of several templates used throughout STL. <iterator> provides many methods used by the iterator, however, the description of <memory> is very difficult. It allocates storage space for elements in the container in an unusual way, and also provides a mechanism for temporary objects generated during Algorithm Execution, the main part of <memory> is the template class Allocator, which is responsible for generating default splitters in all containers.

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.