C++STL (standard template library) theoretical basis

1 Basic concept
STL (Standard Template Library) is a collective term for a series of software developed by HP Labs. It appears mainly in C++, but the technology has existed for a long time before it was introduced into C++.
STL is broadly divided into three categories: algorithm (algorithm), container (container) and iterator (iterator). Containers and algorithms can be seamlessly connected through iterators. Almost all code uses template classes and template functions, which provides better code reuse opportunities than traditional libraries consisting of functions and classes. In the C++ standard, STL is organized into the following 13 header files:,,,,,,、、、、、 和。
在这里插入图片描述
STL详细的说六大组件
–容器(Container)
–算法(Algorithm)
–迭代器(Iterator)
–仿函数(Function object)
–适配器(Adaptor)
空间配制器(allocator)
使用STL的好处
1)STL是C++的一部分,因此不用额外安装什么,它被内建在你的编译器之内。
2)STL的一个重要特点是数据结构和算法的分离。尽管这是个简单的概念,但是这种分离确实使得STL变得非常通用。
例如,在STL的vector容器中,可以放入元素、基础数据类型变量、元素的地址;
STL的sort()函数可以用来操作vector,list等容器。
3)程序员可以不用思考STL具体的实现过程,只要能够熟练使用STL就OK了。这样他们就可以把精力放在程序开发的别的方面。
4)STL具有高可重用性,高性能,高移植性,跨平台的优点。
高可重用性:STL中几乎所有的代码都采用了模板类和模版函数的方式实现,这相比于传统的由函数和类组成的库来说提供了更好的代码重用机会。关于模板的知识,已经给大家介绍了。
高性能:如map可以高效地从十万条记录里面查找出指定的记录,因为map是采用红黑树的变体实现的。(红黑树是平横二叉树的一种)
高移植性:如在项目A上用STL编写的模块,可以直接移植到项目B上。
跨平台:如用windows的Visual Studio编写的代码可以在Mac OS的XCode上直接编译。
5)程序员可以不用思考STL具体的实现过程,只要能够熟练使用STL就OK了。这样他们就可以把精力放在程序开发的别的方面。
6)了解到STL的这些好处,我们知道STL无疑是最值得C++程序员骄傲的一部分。每一个C++程序员都应该好好学习STL。只有能够熟练使用STL的程序员,才是好的C++程序员。
7)总之:招聘工作中,经常遇到C++程序员对STL不是非常了解。大多是有一个大致的映像,而对于在什么情况下应该使用哪个容器和算法都感到比较茫然。STL是C++程序员的一项不可或缺的基本技能,掌握它对提升C++编程大有裨益。

2
In the actual development process of the container, the importance of the data structure itself will not be less important than the importance of the algorithm operating on the data structure. When there are parts that require high time in the program, the choice of data structure becomes even more important. important.
  The number of classic data structures is limited, but we often repeat some codes written to implement structures such as vectors and linked lists. These codes are very similar, but they differ in details to adapt to changes in different data. The STL container provides us with such convenience. It allows us to reuse existing implementations to construct our own data structure under a specific type. By setting up some templates, the STL container provides support for the most commonly used data structures. Parameters allow us to specify the data type of the elements in the container, which can simplify many of our repetitive and tedious work.
The container part is mainly composed of header files,,,,, 和组成。对于常用的一些容器和容器适配器(可以看作由其它容器实现的容器),可以通过下表总结一下它们和相应头文件的对应关系。
2.1容器的概念
用来管理一组元素
在这里插入图片描述
1.2容器的分类
序列式容器(Sequence containers)
每个元素都有固定位置--取决于插入时机和地点,和元素值无关。
vector、deque、list
关联式容器(Associated containers)
元素位置取决于特定的排序准则,和插入顺序无关
set、multiset、map、multimap
在这里插入图片描述
1.3迭代器
迭代器从作用上来说是最基本的部分,可是理解起来比前两者都要费力一些。软件设计有一个基本原则,所有的问题都可以通过引进一个间接层来简化, 这种简化在STL中就是用迭代器来完成的。概括来说,迭代器在STL中用来将算法和容器联系起来,起着一种黏和剂的作用。几乎STL提供的所有算法都是通 过迭代器存取元素序列进行工作的,每一个容器都定义了其本身所专有的迭代器,用以存取容器中的元素。
  迭代器部分主要由头文件,和组 成。是一个很小的头文件,它包括了贯穿使用在STL中的几个模板的声明,中提供了迭代器 使用的许多方法,而对于的描述则十分的困难,它以不同寻常的方式为容器中的元素分配存储空间,同时也为某些算法执行期间产生 的临时对象提供机制,中的主要部分是模板类allocator,它负责产生所有容器中的默认分配器。

1.4 Algorithm
The selection of data types by the function library plays a vital role in its reusability. For example, a function for finding a square root is definitely more reusable when using a floating-point number as its parameter type than using an integer as its parameter type. And C++ through the template mechanism allows to postpone the selection of certain types, until you really want to use the template or specialize the template, STL takes advantage of this and provides quite a lot of useful algorithms. It completes these algorithms in an effective framework-all types can be divided into a few categories, and then one type can be used in the template parameters to replace other types in the same category.
  STL provides about 100 template functions that implement algorithms. For example, the algorithm for_each will call the specified function for each element in the specified sequence, stable_sort will sort the sequence stably according to the rules you specify, and so on. In this way, as long as you are familiar with STL, many codes can be greatly simplified, and only by calling one or two algorithm templates, you can complete the required functions and greatly improve efficiency.
The algorithm part is mainly composed of header files, and. It is the largest of all STL header files (although it is well understood). It is composed of a large number of template functions. It can be considered that each function is largely independent, and the commonly used functions involve To compare, swap, find, traverse operations, copy, modify, remove, reverse, sort, merge, etc. The volume is very small, including only a few template functions that perform simple mathematical operations on the sequence, including some operations on the sequence of addition and multiplication. Some template classes are defined in, to declare function objects.

1.5
C ++ Standard Library The powerful function of C++ comes from its rich class library and library function resources. The content of the C++ standard library is defined in a total of 50 standard header files. In C++ development, the standard library should be used as much as possible. The direct benefits of this include: (1) Cost: It has been provided as a standard, so why bother to spend time and manpower to re-develop it; (2) Quality: The standard library is strictly tested and its correctness is guaranteed; (3) Efficiency: Human efficiency has already been reflected in the cost. Regarding code execution efficiency, we must believe in the level of the big cows who implement the standard library; (4) Good programming style: Use common practices in the industry for development.
In the C++ programming course, especially as the first programming course, we pay attention to the content of grammar and language mechanism. There is a process of cultivating program design ability. It is the common practice to directly enter the engineering through the basic principle knowledge, because the span determines its difficulty. Furthermore, on the basis of mastering the basic principles, the problem of understanding the standard library can be grasped step by step with practice. The study of the standard library does not require serious reading, but in-depth practice while understanding the general situation.
This task is to know this part of the content that is not covered in the C++ programming course, but is very important to programming. At least we need to be able to answer the "what's there" question first.

The content of the C++ standard library is divided into 10 categories, which are (it is recommended to mark out the header files you have used or heard of during reading):  
C1. The header files related to language support functions in the standard library
Header file description
Define the macro NULL and offsetof, as well as other standard types size_t and ptrdiff_t. The difference from the corresponding standard C header file is that NULL is a supplementary definition of C++ null pointer constants. The macro offsetof accepts structure or union type parameters, as long as they have no non-static members of member pointer types.
Provide definitions related to basic data types. For example, for each numeric data type, it defines the maximum and minimum values ​​that can be represented and the number of binary digits.
Provides C style definitions related to basic integer data types. The C++ style definition of this information is
provided in the C style definition related to the basic floating-point data type. The C++ style of this information is defined in
Macros and functions that support program startup and termination. This header file also declares many other miscellaneous functions, such as search and sort functions, conversion from strings to numbers, and other functions. It is different from the corresponding standard C header file stdlib.h, which defines abort(void). The abort() function has additional functions. It does not call the destructor for static or automatic objects, nor does it call the function passed to the atexit() function. It also defines the extra function of the exit() function, which can release static objects and call the functions registered with atexit() in the reverse order of registration. Clear and close all open C streams, and return control to the host environment.
Support dynamic memory allocation
Support variable type identification during operation
Support exception handling, which is a way to handle errors that may occur in the program
Support functions that accept a variable number of parameters. That is, when the function is called, a unequal number of data items can be sent to the function. It defines the macro va_arg, va_end, va_start and va_list types
Provides functions for C-style non-local jumps. These functions are not commonly used in C++.
Provide C-style support for interrupt handling
. C2. Header files that support stream input/output. The
header file description
supports the input and output of standard streams cin, cout, cerr and clog. It also supports multi-byte character standards Stream wcin, wcout, wcerr and wclog.
Provide a manipulation program, allowing to change the state of the stream, thereby changing the output format.
Define the base class
of iostream. Define the template class
for managing the input of the output stream buffer. Define the template class for managing the output of the output stream buffer.
Support the stream input and output of strings. Support the stream input and output of
files.
Provide forward declaration
support for input and output objects. Cache
of stream input and output Provides C-style input and output for standard streams
Supports C-style input and output of multi-byte characters

C3. Header files related to diagnostic functions
Header file description
Defines standard exceptions. Exceptions are the way to handle errors.
Define assertion macros to check the situation during runtime.
Support C-style error messages

C4. The header file that defines the tool function. The header file
description
defines overloaded relational operators to simplify the writing of relational operators. It also defines the pair type, which is a template type that can store a pair of values. These functions are used elsewhere in the library.
Many function object types and functions that support function objects are defined. Function objects are arbitrary objects that support the operator()() function call operator.
Define standards for containers, memory management functions, and auto_ptr template classes. Memory allocator
supports system clock function

C5. Header file supporting string processing
Header file description
Provide support and definition for string types, including single-byte strings (composed of char) and multi-byte strings (composed of wchar_t)
single-byte character categories
The multibyte character category
provides functions for processing non-empty byte sequences and memory blocks. This is different from the corresponding standard C library header file. Several general C library functions of C style strings are replaced by function pairs that return const and non-const.
Provided for processing, performing I/O, and converting multi-byte character sequences Function, which is different from the corresponding standard C library header file. Several general C library functions for multi-byte C-style string manipulation are replaced by pairs of functions that return const and non-const.
Provides functions for converting single-byte strings to numeric values, and between multi-byte characters and multi-byte strings

C6. The header file that
defines the template of the container class defines the vector sequence template, which is an array type whose size can be reset. It is safer and more flexible to
define the list sequence template than ordinary arrays . This is a linked list of sequences, often in any position Insert and delete elements
Define deque sequence templates to support efficient insertion and deletion operations at the beginning and end.
Define sequence adapters queue and priority_queue
for queue (first in first out) data structure Define sequence adapter stack for stack (last in first out) data structure
map是一个关联容器类型,允许根据键值是唯一的,且按照升序存储。multimap类似于map,但键不是唯一的。
set是一个关联容器类型,用于以升序方式存储唯一值。multiset类似于set,但是值不必是唯一的。
为固定长度的位序列定义bitset模板,它可以看作固定长度的紧凑型bool数组

C7. Header file supporting iterators
Header file description
Provide definition and support for iterators

C8. Header files related to algorithms
Header file description
Provides a set of algorithm-based functions, including substitution, sorting, merging and searching.
Declares the C standard library functions bsearch() and qsort(). Searching and sorting
allows using and instead of code. &&

C9. Header files related to numerical operations. Header file
description
Supports the definition and operation of complex numerical values.
Supports numerical vector operations
. Defines a set of general mathematical operations on numerical sequences, such as accumulate and inner_product.
This is a C math library with additional overloads. Functions to support C++ conventions. The
provided functions can extract the absolute value of an integer and perform the remainder operation on the integer

C10. Header files related to localization
Header file description
The localization provided includes character category, sorting sequence, currency and date representation.
Provide C style support for localization

All header files in the C++ standard library have no extension. The C++ standard library is provided in the form of standard header files. In the standard header file, the names related to the macro are defined in the global scope, and other names are declared in the std namespace. In C++, you can also use the standard C library header file name in the form of name.h
** A
brief review of 1.6 templates ** Templates are
a tool for implementing code reuse mechanisms. The essence is to implement type parameterization, that is, to define types as parameters .
C++ provides two kinds of templates: function template, class template

Introduction to
function template  A function template is to create a general function. The function return type and formal parameter type are not specified specifically, but are represented by virtual types.
Any function with the same function body can be replaced by a function template, no need to define multiple functions, just define once in the template.
When calling a function, the system will replace the virtual type in the template according to the type of the actual parameter, thus realizing the functions of different functions.
Introduction to the class template
 Let’s take a look at the following class first. The class for finding the maximum value.
 Like the function template, the class template is to create a general class whose data member types, member function return types, and parameter types are all available Not specified specifically, but represented by virtual types.
 When using a class template to create an object, the system will replace the virtual type in the class template according to the type of the actual parameter, so as to realize the functions of different classes.

Guess you like

Origin blog.csdn.net/it_xiangqiang/article/details/109215971