C / C ++ Standard Library and the Standard Template Library

Transfer: https://www.cnblogs.com/ht-927/p/4726556.html

Powerful C ++ class library from its rich resources and library functions. Content C ++ Standard Library total of 50 standard defined in the header file. In C ++ development, we should take advantage of the standard library complete as possible. Direct benefits of doing so include: (1) Cost: Already as standard, why bother to spend time and manpower to re-develop it; (2) Quality: Standard libraries are rigorously tested, guaranteed correctness; (3) efficiency: efficiency on people already reflected in the cost, and efficiency in the implementation of the code on to believe standard library implementation of large cattle level; (4) good programming style: a common practice in the industry to develop.

  In the C ++ programming course, especially as the first programming course, we focus on the content of grammar, language and other mechanisms. Training program design capabilities have a process, across the basic principle of universal knowledge directly into practice project, due to the span determine its degree of difficulty. Furthermore, in the grasp of the basic principles on the basis of practice can rely entirely on the issue of understanding the standard library, and gradually mastered. The standard library does not need to learn to read in earnest, we need to understand that in the case of an overview, in-depth practice.

  The task is to know C ++ programming course do not speak, but for programming and very important part of this. At least we should be able to answer the "what" question.

 

  A, C ++ Standard Library

  The contents of the C ++ standard library is divided into 10 categories, namely (recommended reading, will you have used or heard header file rowed out):

  C1. Standard library associated with language support header files

head File

Description

<cstddef>

Define the macro NULL and offsetof, as well as other standard types size_t and ptrdiff_t. The difference between the corresponding standard C header files is, NULL are supplements C ++ null pointer constants, macros offsetof accept the structure or union type parameters, if they're not members of non-static member pointer type can be.

<limits>

Provide definitions related to the basic data types. For example, for each numeric data type, which defines the number of bits can be represented as well as the maximum and minimum binary digits.

<climits>

C provide basic style definitions associated with the integer data type. The C ++ style information defined in the <limits> in

<cfloat>

C provides the basic style definitions associated with floating-point data types. The C ++ style information defined in the <limits> in

<cstdlib>

Support program start and termination of macros and functions. The header also declares a number of other miscellaneous functions, such as searching and sorting functions, functions from string to numeric values. It corresponds to the standard C header files stdlib.h different, defined abort (void). abort () function has the additional feature that the object is not static or automatically calls the destructor does not call passed atexit () function is a function. It also defines the exit () function additional function, can release static object, function calls in reverse order to register with atexit () registered. Clear and close all open flow of C, returns control to the host environment.

<new>

Support for dynamic memory allocation

<typeinfo>

Support variable type identifier during operation

<exception>

Support for exception handling, this is the wrong way to deal with a program that can occur

<cstdarg>

Support functions accept a variable number of arguments. That is, when you call the function, you can give varying amounts of transfer function data items. It defines the macro va_arg, va_end, va_start and type va_list

<csetjmp>

C is non-local-style jump provides functions. These functions are not commonly used in C ++

<csignal>

Provide support for the C-style interrupt processing

  C2. Support stream input / output header files

head File

Description

<iostream>

Supports standard flow cin, cout, cerr and clog input and output, it also supports multi-byte character standard flow wcin, wcout, wcerr and wclog.

<iomanip>

Providing manipulation that allows changing the flow state, thereby changing the output format.

<ios>

Base class definitions iostream

<istream>

Enter the template class is defined as the output stream buffer zone management

<ostream>

Template class is defined as the output buffer of the output stream management

<sstream>

Support stream input output string

<fstream>

Stream input and output file support

<iosfwd>

Provide a statement ahead of the input and output objects

<streambuf>

Supports caching stream input and output

<cstdio>

C to provide a standard style for the input and output streams

<cwchar>

Support for multi-byte characters in C-style input and output

  C3. Associated with diagnostic function header files

head File

Description

<stdexcept>

The definition of the standard exceptions. An exception is the wrong way to deal with

<cassert>

Assert macro is defined, during the inspection operation for the case

<cerrno>

It supports C-style error message

  C4. Header files define utility functions

head File

Description

<utility>

Defined overloaded relational operators, relational operators to simplify the writing, it defines a pair type, which is a template type, a pair of values ​​may be stored. These functions use elsewhere library

<functional>

Defines a number of object types and functions to support the function of the function object, the object function is to support operator () () function call operator any object

<memory>

To the container, the memory management functions and auto_ptr template class definition standard memory allocator

<ctime>

Support System clock function

  C5. Support string processing headers

head File

Description

<string>

Support and defined as a string type, comprising a single-byte character string (char consisting of a) a multi-byte strings and string (composed of wchar_t)

<cctype>

Single-byte character classes

<cwctype>

Multi-byte character classes

<cstring>

Processing function to provide a non-null byte sequences and the memory block. This is different from the corresponding standard C library header files, library functions generally C several C-style string and const is the return value of the function replaces the non-const

<cwchar>

For the processing, performs I / O and switching functions to provide the multi-byte character sequences, which is different from the corresponding standard C library header files, library functions generally C multibyte several C-style string operation is a non-return value const and const function of replacing.

<cstdlib>

为把单字节字符串转换为数值、在多字节字符和多字节字符串之间转换提供函数

  C6. 定义容器类的模板的头文件

头文件

描        述

<vector>

定义vector序列模板,这是一个大小可以重新设置的数组类型,比普通数组更安全、更灵活

<list>

定义list序列模板,这是一个序列的链表,常常在任意位置插入和删除元素

<deque>

定义deque序列模板,支持在开始和结尾的高效插入和删除操作

<queue>

为队列(先进先出)数据结构定义序列适配器queue和priority_queue

<stack>

为堆栈(后进先出)数据结构定义序列适配器stack

<map>

map是一个关联容器类型,允许根据键值是唯一的,且按照升序存储。multimap类似于map,但键不是唯一的。

<set>

set是一个关联容器类型,用于以升序方式存储唯一值。multiset类似于set,但是值不必是唯一的。

<bitset>

为固定长度的位序列定义bitset模板,它可以看作固定长度的紧凑型bool数组

  C7. 支持迭代器的头文件

头文件

描        述

<iterator>

给迭代器提供定义和支持

  C8. 有关算法的头文件

头文件

描        述

<algorithm>

提供一组基于算法的函数,包括置换、排序、合并和搜索

<cstdlib>

声明C标准库函数bsearch()和qsort(),进行搜索和排序

<ciso646>

允许在代码中使用and代替&&

  C9. 有关数值操作的头文件

头文件

描        述

<complex>

支持复杂数值的定义和操作

<valarray>

支持数值矢量的操作

<numeric>

在数值序列上定义一组一般数学操作,例如accumulate和inner_product

<cmath>

这是C数学库,其中还附加了重载函数,以支持C++约定

<cstdlib>

提供的函数可以提取整数的绝对值,对整数进行取余数操作

  C10. 有关本地化的头文件

头文件

描        述

<locale>

提供的本地化包括字符类别、排序序列以及货币和日期表示。

<clocale>

对本地化提供C样式支持

  C++标准库的所有头文件都没有扩展名。C++标准库以<cname>形式的标准头文件提供。在 <cname>形式标准的头文件中,与宏相关的名称在全局作用域中定义,其他名称在std命名空间中声明。在C++中还可以使用name.h 形式的标准C库头文件名。

 

  二、标准模板库STL简介[1]

  STL(Standard Template Library,标准模板库)是惠普实验室开发的一系列软件的统称。现然主要出现在C++中,但在被引入C++之前该技术就已经存在了很长的一段时间。

  STL的代码从广义上讲分为三类:algorithm(算法)、container(容器)和iterator(迭代器),几乎所有的代码都采 用了模板类和模版函数的方式,这相比于传统的由函数和类组成的库来说提供了更好的代码重用机会。在C++标准中,STL被组织为下面的13个头文 件:<algorithm>、<deque>、<functional>、<iterator>、<vector>、<list>、<map>、<memory>、<numeric>、<queue>、<set>、<stack> 和<utility>。

  1、算法

  函数库对数据类型的选择对其可重用性起着至关重要的作用。举例来说,一个求方根的函数,在使用浮点数作为其参数类型的情况下的可重用性肯定比使 用整型作为它的参数类性要高。而C++通过模板的机制允许推迟对某些类型的选择,直到真正想使用模板或者说对模板进行特化的时候,STL就利用了这一点提 供了相当多的有用算法。它是在一个有效的框架中完成这些算法的——可以将所有的类型划分为少数的几类,然后就可以在模版的参数中使用一种类型替换掉同一种 类中的其他类型。

  STL提供了大约100个实现算法的模版函数,比如算法for_each将为指定序列中的每一个元素调用指定的函数,stable_sort以 你所指定的规则对序列进行稳定性排序等等。这样一来,只要熟悉了STL之后,许多代码可以被大大的化简,只需要通过调用一两个算法模板,就可以完成所需要 的功能并大大地提升效率。

  算法部分主要由头文件<algorithm>,<numeric>和<functional>组 成。<algorithm>是所有STL头文件中最大的一个(尽管它很好理解),它是由一大堆模版函数组成的,可以认为每个函数在很大程度上 都是独立的,其中常用到的功能范围涉及到比较、交换、查找、遍历操作、复制、修改、移除、反转、排序、合并等等。<numeric>体积很 小,只包括几个在序列上面进行简单数学运算的模板函数,包括加法和乘法在序列上的一些操作。<functional>中则定义了一些模板类, 用以声明函数对象。

  2、容器

  在实际的开发过程中,数据结构本身的重要性不会逊于操作于数据结构的算法的重要性,当程序中存在着对时间要求很高的部分时,数据结构的选择就显得更加重要。

  经典的数据结构数量有限,但是我们常常重复着一些为了实现向量、链表等结构而编写的代码,这些代码都十分相似,只是为了适应不同数据的变化而在 细节上有所出入。STL容器就为我们提供了这样的方便,它允许我们重复利用已有的实现构造自己的特定类型下的数据结构,通过设置一些模版类,STL容器对 最常用的数据结构提供了支持,这些模板的参数允许我们指定容器中元素的数据类型,可以将我们许多重复而乏味的工作简化。

  容器部分主要由头文 件<vector>,<list>,<deque>,<set>,<map>,<stack> 和<queue>组成。对于常用的一些容器和容器适配器(可以看作由其它容器实现的容器),可以通过下表总结一下它们和相应头文件的对应关 系。

数据结构

描述

实现头文件

向量(vector)

连续存储的元素

<vector>

列表(list)

由节点组成的双向链表,每个结点包含着一个元素

<list>

双队列(deque)

连续存储的指向不同元素的指针所组成的数组

<deque>

集合(set)

由节点组成的红黑树,每个节点都包含着一个元素,节点之间以某种作用于元素对的谓词排列,没有两个不同的元素能够拥有相同的次序

<set>

多重集合(multiset)

允许存在两个次序相等的元素的集合

<set>

栈(stack)

后进先出的值的排列

<stack>

队列(queue)

先进先出的执的排列

<queue>

优先队列(priority_queue)

元素的次序是由作用于所存储的值对上的某种谓词决定的的一种队列

<queue>

映射(map)

由{键,值}对组成的集合,以某种作用于键对上的谓词排列

<map>

多重映射(multimap)

允许键对有相等的次序的映射

<map>

  3、迭代器

  迭代器从作用上来说是最基本的部分,可是理解起来比前两者都要费力一些。软件设计有一个基本原则,所有的问题都可以通过引进一个间接层来简化, 这种简化在STL中就是用迭代器来完成的。概括来说,迭代器在STL中用来将算法和容器联系起来,起着一种黏和剂的作用。几乎STL提供的所有算法都是通 过迭代器存取元素序列进行工作的,每一个容器都定义了其本身所专有的迭代器,用以存取容器中的元素。

  迭代器部分主要由头文件<utility>,<iterator>和<memory>组 成。<utility>是一个很小的头文件,它包括了贯穿使用在STL中的几个模板的声明,<iterator>中提供了迭代器 使用的许多方法,而对于<memory>的描述则十分的困难,它以不同寻常的方式为容器中的元素分配存储空间,同时也为某些算法执行期间产生 的临时对象提供机制,<memory>中的主要部分是模板类allocator,它负责产生所有容器中的默认分配器。

 

  三、后记

  对于STL的使用,也普遍存在着两种观点。第一种认为STL的最大作用在于充当经典的数据结构和算法教材,因为它的源代码涉及了许多具体实现方 面的问题。第二种则认为STL的初衷乃是为了简化设计,避免重复劳动,提高编程效率,因此应该是“应用至上”的,对于源代码则不必深究。对于初学者而言, 通过分析源代码,提高对其应用的理解其意义也不同凡响。

  曾经想着设计几个上机题目,让同学们体会一下利用STL编程。写出一个适合初学者的,规模又不能太大,还要有足够引导的题目,实在是一件非常费时费力 的事,加上有其他事还得应急,就将此帐欠下,日后再说。要给同学们提的建议是,不少C++的经典教材对STL都有非常好的讲解,可以选一本去读。在读书 时,要开始学着挑着读,跳着读,不必从头到尾,逐页去读。在这个阶段,可以首先学习迭代器utility、在C++编程中建议替代数组的vector,以 及实现双向链表的list。vector和list与本周任务1和任务2似乎有些相关。再者,发扬我们一贯特别能实践的精神,及时找些题目或者自编题目进 行实践。

Guess you like

Origin blog.csdn.net/weixin_43996899/article/details/91986221