Team LiB
Previous Section Next Section

Chapter 3. Library Types

第三章 标准库类型

CONTENTS

Section 3.1 Namespace using Declarations

78

Section 3.2 Library string Type

80

Section 3.3 Library vector Type

90

Section 3.4 Introducing Iterators

95

Section 3.5 Library bitset Type

101

Chapter Summary

107

Defined Terms

107

In addition to the primitive types covered in Chapter 2, C++ defines a rich library of abstract data types. Among the most important library types are string and vector, which define variable-sized character strings and collections, respectively. Associated with string and vector are companion types known as iterators, which are used to access the characters in a string or the elements in a vector. These library types are abstractions of more primitive typesarrays and pointersthat are part of the language.

第二章介绍的基本数据类型外,C++ 还定义了一个内容丰富的抽象数据类型标准库。其中最重要的标准库类型是 stringvector,它们分别定义了大小可变的字符串和集合。stringvector 往往将迭代器用作配套类型(companion type),用于访问 string 中的字符,或者 vector 中的元素。这些标准库类型是语言组成部分中更基本的那些数据类型(如数组和指针)的抽象。

Another library type, bitset, provides an abstract way to manipulate a collection of bits. This class provides a more convenient way of dealing with bits than is offered by the built-in bitwise operators on values of integral type.

另一种标准库类型 bitset,提供了一种抽象方法来操作位的集合。与整型值上的内置位操作符相比,bitset 类类型提供了一种更方便的处理位的方式。

This chapter introduces the library vector, string, and bitset types. The next chapter covers arrays and pointers, and Chapter 5 looks at built-in bitwise operators.

本章将介绍标准库中的 vectorstringbitset 类型。第四章将讨论数组和指针第五章将讲述内置位操作符。

The types that we covered in Chapter 2 are all low-level types: They represent abstractions such as numbers or characters and are defined in terms of how they are represented on the machine.

第二章所涉及的类型都是低层数据类型:这些类型表示数值或字符的抽象,并根据其具体机器表示来定义。

In addition to the types defined in the language, the standard library defines a number of higher level abstract data types. These library types are higher-level in that they mirror more complex concepts. They are abstract because when we use them we don't need to care about how the types are represented. We need to know only what operations they support.

除了这些在语言中定义的类型外,C++ 标准库还定义了许多更高级的抽象数据类型之所以说这些标准库类型是更高级的,是因为其中反映了更复杂的概念;之所以说它们是抽象的,是因为我们在使用时不需要关心它们是如何表示的,只需知道这些抽象数据类型支持哪些操作就可以了。

Two of the most important library types are string and vector. The string type supports variable-length character strings. The vector type holds a sequence of objects of a specified type. These types are important because they offer improvements over more primitive types defined by the language. Chapter 4 looks at the language-level constructs that are similar to, but less flexible and more error-prone than, the library string and vector types.

两种最重要的标准库类型是 stringvectorstring 类型支持长度可变的字符串,vector 可用于保存一组指定类型的对象。说它们重要,是因为它们在 C++ 定义的基本类型基础上作了一些改进。第四章还将学习类似于标准库中 stringvector 类型的语言级构造,但标准库的 stringvector 类型可能更灵活,且不易出错。

Another library type that offers a more convenient and reasonably efficient abstraction of a language level facility is the bitset class. This class lets us treat a value as a collection of bits. It provides a more direct way of operating on bits than do the bitwise operators that we cover in Section 5.3 (p. 154).

另一种标准库类型提供了更方便和合理有效的语言级的抽象设施,它就是 bitset 类。通过这个类可以把某个值当作们的集合来处理。与 第 5.3 节介绍的位操作符相比,bitset 类提供操作位更直接的方法。

Before continuing our exploration of the library types, we'll look at a mechanism for simplifying access to the names defined in the library.

在继续探究标准库类型之前,我们先看一种机制,这种机制能够简化对标准库中所定义名字的访问。


Team LiB
Previous Section Next Section