Introduction to C ++ STL

Source: Internet
Author: User

Author: the robe of anger

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 ++.
In a broad sense, STL code is divided into three types: algorithm (algorithm), container (container), and iterator (iterator). Almost all the Code uses the template class and template function, 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. When the program has a time-demanding part, the selection of data structures becomes more important.

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.

5. Some suggestions for beginners to learn STL
For readers who didn't know much about STL before, the above text only briefly describes the STL framework, which is of little help for you to understand the STL mechanism and even use STL, this is not only because you need to have a comprehensive understanding of C ++ advanced applications, even more, the three parts of STL, algorithm, container, and iterator, are mutually restrained or closely integrated. In terms of concept, the most basic part is the iterator, but directly learning the iterator will encounter many abstract and tedious details, however, if you do not really understand the iterator, you cannot directly learn the other two parts (at least for the source code analysis ). It can be said that it takes some time to adapt to STL to solve the problem. However, at this cost, STL achieved a very valuable independence, it can use the iterator to complete the operation of this structure with as few data structures as possible, so it is determined that friends who study STL should never be knocked down by the temporary difficulties. In fact, STL uses a relatively unified mode. As long as it adapts to it, there will be no major changes from one STL tool to another.

There are also two common points of view on the use of STL. First, I think the biggest role of STL is to act as a classic data structure and algorithm textbook, because its source code involves many specific implementation problems. The second is that the original intention of STL is to simplify the design, avoid repetitive work, and improve programming efficiency. Therefore, it should be "application first" and do not have to go into the source code. The author believes that the source code analysis is not in conflict with the application. By analyzing the source code, we can also improve our understanding of its application. Of course, we can also have different focuses based on specific purposes.

STL is part of the ANSI/iso c ++ standard, the first consideration should be the template provided by STL (efficient and portable), followed by the corresponding libraries of various vendors (efficient but not portable) and write your 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.