C++ STL Standard Template Library Introduction and Getting Started

Table of contents

1 Overview

1.1. C++ standard library

1.2. Boost library

2. STL version

2.1, HP original version

2.2, PJ implementation version

2.3, RW implementation version

2.4, SGI implementation version

2.5, STLport implementation version

3. Six components of STL

3.1. Composition of six major components of STL

3.2, the interaction relationship between the six major components

4. The importance of STL

5. Summary


VC++ common function development summary (column article list, welcome to subscribe, continuous update...) https://blog.csdn.net/chenlycly/article/details/124272585 C++ software exception troubleshooting series tutorial from entry to mastery (column article list , Welcome to subscribe, keep updating...) https://blog.csdn.net/chenlycly/article/details/125529931 C++ software analysis tool case collection (updating...) https://blog.csdn.net /chenlycly/category_12279968.html?spm=1001.2014.3001.5482        The STL standard template library is an important part of the C++ standard library and is widely used in C++ code. Today, let’s talk about the relevant content of the STL standard template library in detail.

1 Overview

       STL (standard template libaray - Standard Template Library), is an important part of the C++ standard library. It is not only a reusable component library, but also a software framework including data structures and algorithms. Almost all codes in STL use template classes and template functions, so there is good code reusability.

STL is a collection of some containers, algorithms, and other components. All containers and algorithms are implemented based on the research results of algorithms and data structures for decades, and the experience of many computer experts and scholars. Therefore, it can be said that STL Basically, it has achieved a high degree of optimization of various storage methods and related algorithms.

       The containers we commonly use in C++ code include vector, list, map, and deque. They are implemented based on templates, which is convenient for us to store various types of data. With these containers, we generally don't need to implement some data structures ourselves, which brings us great convenience and effectively improves our coding efficiency. In addition to containers, STL also provides algorithmic functions for operating these containers. Using these efficient algorithmic functions is much more efficient than traversing containers directly. For this, you can check out my previous writing based on the usage examples and experience in the project. article:

VC++ calls the STL algorithm function to effectively improve the search speed of the STL list (with source code) https://blog.csdn.net/chenlycly/article/details/123943134

Some people may think that STL only contains container-related content. In fact, smart pointers such as string, input and output iostream, and unique_ptr that you usually use are all in the STL standard template library. The STL template library contains more than just containers and iterators.

       In addition, when it comes to STL, it is necessary to talk about the C++ standard library; when it comes to the C++ standard library, it is necessary to talk about the C++ "quasi" standard library Boost library. So here is a brief introduction to the C++ standard library and Boost library.

1.1. C++ standard library

        The C++ Standard Library (C++ Standard Library) provides a wealth of class libraries and library function resources, which are defined in a total of 50 standard header files, including language support, input and output, general tools, string operations, containers, iterators, Algorithmic functions, numerical operations, etc.

       The C++ standard library is mainly composed of the C library, the C++ library and the STL standard template library, of which the STL standard template library accounts for about 80% of the C++ standard library. In C++ software development, use the resources in the C++ standard library as much as possible to complete.

1.2, Boost open source library

       The Boost open source library is developed and maintained by the Boost community established by some members of the C++ Standards Committee. It uses many modern C++ programming techniques, including string processing, regular expressions, containers and data structures, concurrent programming, functional programming, Many areas such as generic programming and design pattern realization have greatly enriched the functions and expressiveness of C++, and can make C++ software development more simple, flexible and efficient.

The Boost library was initiated by members of the C++ Standards Committee library working group, that is, many authors of the Boost library are themselves members of the C++ Standards Committee. Therefore, Boost "naturally" becomes the backup of the standard library, responsible for delivering components to the new standard, which also makes Boost obtain Gained the reputation of "quasi" standard library!

       The C++ standard library introduces the well-known regular expression regex library from the boost library , the smart pointer unique_ptr (corresponding to the scoped_ptr in the boost library) , shared_ptr and weak_ptr , function adaptation bind library , function object container function , etc.

2. STL version

       Since the ANSI/ISO C++ standard was officially finalized in 1998 and the C++ STL specification version was officially passed, various C++ compiler manufacturers have implemented C++ STL generic libraries that meet their own needs on the basis of this standard, mainly including HP STL, PJ STL , Rouge Wave STL, SGI STL, etc.

2.1, HP original version

       The HP STL was created by Alexandar Stepanov (the father of the STL Standard Template Library) in collaboration with Meng Lee while working at the HP Palo Alto Lab. In the spirit of open source, they declare that anyone is allowed to use, copy, modify, disseminate, and commercially use these codes without payment. The only thing to abide by is that HP's version statement and use permission statement must be added to the file. 

HP STL is the first implementation version of C++ STL, and other versions of C++ STL are generally implemented based on HP STL. However, this version of the STL is rarely used directly these days.

2.2, PJ implementation version

       Developed by PJ Plauger and inherited from the HP version, this version is not open source and cannot be released, modified or sold. It is also legal that this version is not open source, because HP did not require that its derivative products must be open source.

       This version is adopted by Microsoft Visual C++ . The disadvantage is that the readability is relatively low and the symbol names are relatively weird. But when we read the implementation source code of this version in Visual Studio, it feels good, and it is not as difficult to read as it is said in the legend.

In fact, PJ STL is a product of PJPlauger Company, although the company had only 3 people at that time.

2.3, RW implementation version

       Developed by Rouge Wage, inherited from the HP version, and adopted by Borland's C++ Builder . This version is also not open source, and cannot be disclosed, modified or sold. The readability of this version is not bad.

       It is worth mentioning that although the performance of Rouge Wave STL is not very good, C++ Builder's support for the C++ language standard is not bad, so the performance of Rouge Wave STL is improved to a certain extent. But unfortunately, because Rouge Wave STL has not been updated for a long time and is not fully compliant with the standard, Rouge Wave STL switched to STLport version in version 6.0 (subsequent versions also use STLport), but considering compatibility with previous versions , Rouge Wave STL is still retained in version 6.0.

The Rouge Wave company should be said to be well-known in the field of C++ libraries, and has contributed a lot to the process of C++ standardization. However, the Rouge Wave STL version is not only slow to update, but also expensive. For these two reasons, Borland decided to abandon Rouge Wave STL and use STLport instead in version 6.0.

2.4, SGI implementation version

       Developed by Silicon Graphics Computer Systems, Inc., inherited from the HP version. This version is adopted by Linux GCC and has good portability and excellent performance on the Linux platform. This version is open source and can be published, modified or even sold.

This version of STL was also developed by Alexander Stepanov (the HP version was developed by him). After Alexander Stepanov left HP, he joined SGI and developed SGI STL with Matt Austern and others.

       Whether it is symbolic naming or programming style, this version is very readable. If you want to learn STL source code, I recommend you to read the source code implementation of this version. Hou Jie's classic book "STL Source Code Analysis" is also based on this version.

2.5, STLport implementation version

      In order to make the basic code of SGI STL applicable to various compilers such as VC++ and C++ Builder, Russian Boris Fomitchev established a free project to develop STLport. This version of STL is open source.

Since Rouge Wave STL has not been updated for a long time and does not fully comply with the standard, Rouge Wave STL switched to STLport version in version 6.0, and all subsequent versions also use STLport.

3. Six components of STL

       The STL standard template library is an important part of the C++ standard library, so all C++ compilers support STL and choose their own STL version. STL provides six major components, which work together in combination with each other.

3.1. Composition of six major components of STL

       The six components are:

  • Containers : Various data structures, such as vector, list, deque, set, map, etc. From an implementation point of view, a container is a class template.
  • Algorithms : Various commonly used algorithms provide ways to perform various operations, including initialization, sorting, searching, and converting operations on container contents, such as sort, search, copy, and erase. From an implementation point of view, an STL algorithm is a function template.
  • Iterators (Iterators) : Iterators are used to traverse the elements of the object collection, acting as the glue between the container and the algorithm, and are the so-called "generic pointers". There are 5 types and other derivative changes. From an implementation point of view, an iterator is a class template that overloads pointer operations such as operator*, operator->, operator++, and operator--. All STL containers come with their own iterators, because only the container designer knows how to iterate over their elements.
  • Functors : Also known as Function objects, they behave like functions and can be used as a strategy for algorithms. From an implementation point of view, a functor is a class or class template that overloads operator().
  • Adaptors : A thing used to decorate a container or a functor or iterator interface. For example, the queue and stack provided by STL are a kind of space adapter, because their bottoms are completely dependent on deque.
  • Allocators : Also known as space configurators, responsible for space configuration and management. From an implementation point of view, the configurator is a class template that implements dynamic configuration space, space management, and space release.

3.2, the interaction relationship between the six major components

        The interaction relationship between the six components is as follows:

3.2.1. Container

       A container is a collection of objects of a specific type. There are two types of containers in STL, sequential containers and associative containers.

       Sequential containers provide programmers with the ability to control the order in which elements are stored and accessed. This order does not depend on the value of the element, but corresponds to the position of the element when it was added to the container.

       In addition to sequential containers, the standard library defines three sequential container adapters: stack, queue, and priority_queue. Adapters are a common concept in the standard library, and containers, iterators, and functions all have adapters. Essentially, an adapter is a mechanism that makes one thing behave like another.

       Corresponding to the serial container is the associative container (associative-container), and the elements in the associative container are stored and accessed by keywords. Associative containers support efficient keyword lookup and access, and the STL has two main associative containers: map and set. 

3.2.2, container iterator

       Iterators provide access to the objects in a container and define the range of objects in the container. An iterator is like a pointer. In fact, a C++ pointer is also a kind of iterator. However, iterators are not just pointers, so you can't assume they have address values. For example, an array index can also be thought of as a kind of iterator.

       Iterators can be created in various ways. A program may create an iterator as a variable. An STL container class may create an iterator for working with data of a particular type. As a pointer, it must be possible to obtain data using the * operator class. You can also use other mathematical operators such as the ++ operator to increment an iterator to access the next object in the container.

If the iterator reaches after the last element in the container, the iterator becomes a past-the-end value. It is illegal to use a past-the-end value pointer to access the object, just like using NULL or uninitialized pointers.

3.2.3 Algorithms

       STL provides many general-purpose algorithms for containers through function templates, such as search, insertion, deletion, sorting, etc. All these algorithms need to import the header file <algorithm>.

       All STL algorithms operate on the interval marked by the iterator [first, last) and can be divided into two categories:

  • Mutating algorithms: During the operation, the content of the element pointed to by the iterator in the interval will be changed, such as partition, remove and other algorithms.
  • Nonmutating algorithms : During the operation, the content of the element pointed to by the iterator in the interval will not be changed, such as matching (search), counting (count) and other algorithms.

       The first two parameters of all generic algorithms are a pair of iterators, usually called first and last, used to mark the operating range of the algorithm. Note that passing an invalid iterator to an algorithm, while an error, is not guaranteed to be caught at compile time.

4. The importance of STL

       To discuss the importance of STL, we must look at it from several dimensions.

       From the perspective of learning , by learning the STL source code implementation, you can learn a lot of classic implementations of data structures, which has a good effect on improving personal abilities.

       From the perspective of interviews , STL also frequently asks topics in interviews, such as comparing the advantages and disadvantages of containers (such as vector and list), precautions when using containers, and the internal implementation mechanism or ideas of certain containers.

       From the perspective of work , STL implements many commonly used data structures and algorithmic functions for operating these data structures. We basically don’t need to reinvent the wheel ourselves. We can stand on the shoulders of our predecessors and develop efficiently and quickly. In your daily C++ code, large-scale use of vector, list, map and other containers and related efficient algorithm functions brings great convenience to daily development work and effectively improves everyone's development efficiency.

5. Summary

       Everyone uses STL containers more in their work, accumulates experience in using STL containers, and understands STL-related knowledge points and precautions during use. If you want to learn STL internal source code and design ideas, I recommend reading the classic book "STL Source Code Analysis" by Mr. Hou Jie.

       Although STL is very powerful and brings us a lot of convenience, STL is also flawed. In order to pursue higher efficiency, the internal implementation of STL is very complicated, such as type extraction, iterator extraction, etc. STL does not support thread safety, and we need to lock ourselves for concurrent operations. In addition, STL is implemented with templates, which ensures the generality and reusability of the code, but it also brings the problem of code expansion. Where STL is used, a copy of the instantiation implementation code will be copied. To a certain extent, there will be code expansion problems, which will increase the size of the binary file.

Guess you like

Origin blog.csdn.net/chenlycly/article/details/130674425