Team LiB
Previous Section Next Section

Part II: Containers and Algorithms

第二部分:容器和算法

We've said that C++ is about efficient programming with abstractions. The Standard Library is a good example: The library defines a number of container classes and a family of generic algorithms that let us write programs that are succinct, abstract, and efficient. The library worries about bookkeeping detailsin particular, taking care of memory managementso that our programs can worry about the actual problems we need to solve.

C++ 提供了使用抽象进行高效率编程的方式。标准库就是一个很好的例子:标准库定义了许多容器类以及一系列泛型算法,使程序员可以更简洁、抽象和有效地编写程序。这样可以让标准库操心那些繁琐的细节,特别是内存管理,我们的程序只需要关注要解决的实际问题就行了。

In Chapter 3 we introduced the vector container type. We'll learn more in Chapter 9 about vector and the other sequential container types provided by the library. We'll also cover more operations provided by the string type. We can think of a string as a special kind of container that contains only characters. The string type supports many, but not all, of the container operations.

第三章介绍了 vector 容器类型。我们将会在第九章进一步探讨 vector 和其他顺序容器类型,而且还会学习 string 类型提供的更多操作,这些容器类型都是由标准库定义的。我们可将 string 视为仅包含字符的特殊容器,string 类型提供大量(但并不是全部)的容器操作。

The library also defines several associative containers. Elements in an associative container are ordered by key rather than sequentially. The associative containers share many operations with the sequential containers and also define operations that are specific to the associative containers. The associative containers are covered in Chapter 10.

标准库还定义了几种关联容器。关联容器中的元素不是顺序排列,而是按键(key)排序的。关联容器共享了许多顺序容器提供的操作,此外,还定义了自己特殊的操作。我们将在第十章学习相关的内容。

Chapter 11 introduces the generic algorithms. The algorithms typically operate on a range of elements from a container or other sequence. The algorithms library offers efficient implementations of various classical algorithms, such as searching, sorting, and other common tasks. For example, there is a copy algorithm, which copies elements from one sequence to another; find, which looks for a given element; and so on. The algorithms are generic in two ways: They they can be applied to different kinds of containers, and those containers may contain elements of most types.

第十一章介绍了泛型算法,这些算法通常作用于容器或序列中某一范围的元素。算法库提供了各种各样经典算法的有效实现,像查找、排序及其他常见的算法任务。例如,复制算法将一个序列中所有所元素复制到另一个序列中;查找算法则用于寻找一个指定元素,等等。泛型算法中,所谓“泛型(generic)”指的是两个方面:这些算法可作用于各种不同的容器类型,而这些容器又可以容纳多种不同类型的元素。

The library is designed so that the container types provide a common interface: If two containers offer a similar operation, then that operation will be defined identically for both containers. For example, all the containers have an operation to return the number of elements in the container. All the containers name that operation size, and they all define a type named size_type that is the type of the value returned by size. Similarly, the algorithms have a consistent interface. For example, most algorithms operate on a range of elements specified by a pair of iterators.

为容器类型提供通用接口是设计库的目的。如果两种容器提供相似的操作,则为它们定义的这个操作应该完全相同。例如,所有容器都有返回容器内元素个数的操作,于是所有容器都将操作命名为 size,并将 size 返回值的类型都指定为 size_type 类型。类似地,算法具有一致的接口。例如,大部分算法都作用在由一对迭代器指定的元素范围上。

Because the container operations and algorithms are defined consistently, learning the library becomes easier: Once you understand how an operation works, you can apply that same operation to other containers. More importantly, this commonality of interface leads to more flexible programs. It is often possible to take a program written to use one container type and change it to use a different container without having to rewrite code. As we'll see, the containers offer different performance tradeoffs, and the ability to change container types can be valuable when fine-tuning the performance of a system.

容器提供的操作和算法是一致定义的,这使得学习标准库更容易:只需理解一个操作如何工作,就能将该操作应用于其他的容器。更重要的是,接口的一致性使程序变得更灵活。通常不需要重新编写代码,就可以将一段使用某种容器类型的程序修改为使用不同容器实现。正如我们所看到的,容器提供了不同的性能折衷方案,可以改变容器类型对优化系统性能来说颇有价值。


CONTENTS
目录
 

Chapter 9 Sequential Containers

 

Chapter 10 Associative Containers

 

Chapter 11 Generic Algorithms

Team LiB
Previous Section Next Section