Team LiB
Previous Section Next Section

Chapter 9. Sequential Containers

CONTENTS

目录

Section 9.1 Defining a Sequential Container

307

Section 9.2 Iterators and Iterator Ranges

311

Section 9.3 Sequence Container Operations

316

Section 9.4 How a vector Grows

330

Section 9.5 Deciding Which Container to Use

333

Section 9.6 strings Revisited

335

Section 9.7 Container Adaptors

348

Chapter Summary

353

Defined Terms

353


This chapter completes our discussion of the standard-library sequential container types. It expands on the material from Chapter 3, which introduced the most commonly used sequential container, the vector type. Elements in a sequential container are stored and accessed by position. The library also defines several associative containers, which hold elements whose order depends on a key. Associative containers are covered in the next chapter.

第三章介绍了最常用的顺序容器:vector 类型。本章将对第三章的内容进行扩充和完善,继续讨论标准库提供的顺序容器类型。顺序容器内的元素按其位置存储和访问。除顺序容器外,标准库还定义了几种关联容器,其元素按键(key)排序。我们将在下一章讨论它们。

The container classes share a common interface. This fact makes the library easier to learn; what we learn about one type applies to another. Each container type offers a different set of time and functionality tradeoffs. Often a program using one type can be fine-tuned by substituting another container without changing our code beyond the need to change type declarations.

容器类共享公共的接口,这使标准库更容易学习,只要学会其中一种类型就能运用另一种类型。每种容器类型提供一组不同的时间和功能折衷方案。通常不需要修改代码,只需改变类型声明,用一种容器类型替代另一种容器类型,就可以优化程序的性能。

A container holds a collection of objects of a specified type. We've used one kind of container already: the library vector type. It is a sequential container. It holds a collection of elements of a single type, making it a container. Those elements are stored and accessed by position, making it a sequential container. The order of elements in a sequential container is independent of the value of the elements. Instead, the order is determined by the order in which elements are added to the container.

容器容纳特定类型对象的集合。我们已经使用过一种容器类型:标准库 vector 类型,这是一种顺序容器(sequential container)。它将单一类型元素聚集起来成为容器,然后根据位置来存储和访问这些元素,这就是顺序容器。顺序容器的元素排列次序与元素值无关,而是由元素添加到容器里的次序决定。

The library defines three kinds of sequential containers: vector, list, and deque (short for "double-ended queue" and pronounced "deck"). These types differ in how elements are accessed and the relative run-time cost of adding or removing elements. The library also provides three container adaptors. Effectively, an adaptor adapts an underlying container type by defining a new interface in terms of the operations provided by the original type. The sequential container adaptors are stack, queue, and priority_queue.

标准库定义了三种顺序容器类型:vectorlist 和 deque(是双端队列“double-ended queue”的简写,发音为“deck”)。它们的差别在于访问元素的方式,以及添加或删除元素相关操作的运行代价。标准库还提供了三种容器适配器(adaptors)。实际上,适配器是根据原始的容器类型所提供的操作,通过定义新的操作接口,来适应基础的容器类型。顺序容器适配器包括 stackqueue 和 priority_queue 类型,见表 9-1

Containers define only a small number of operations. Many additional operations are provided by the algorithms library, which we'll cover in Chapter 11. For those operations that are defined by the containers, the library imposes a common interface. The containers vary as to which operations they provide, but if two containers provide the same operation, then the interface (name and number of arguments) will be the same for both container types. The set of operations on the container types form a kind of hierarchy:

容器只定义了少量操作。大多数额外操作则由算法库提供,我们将在第十一章学习算法库。标准库为由容器类型定义的操作强加了公共的接口。这些容器类型的差别在于它们提供哪些操作,但是如果两个容器提供了相同的操作,则它们的接口(函数名字和参数个数)应该相同。容器类型的操作集合形成了以下层次结构:

  • Some operations are supported by all container types.

    一些操作适用于所有容器类型。

  • Other operations are common to only the sequential or only the associative containers.

    另外一些操作则只适用于顺序或关联容器类型。

  • Still others are common to only a subset of either the sequential or associative containers.

    还有一些操作只适用于顺序或关联容器类型的一个子集。

In the remainder of this chapter, we look at the sequential container types and their operations in detail.

在本章的后续部分,我们将详细描述顺序容器类型和它们所提供的操作。

Table 9.1. Sequential Container Types
表 9.1. 顺序容器类型

Sequential Containers

顺序容器

 

vector

Supports fast random access

支持快速随机访问

list

Supports fast insertion/deletion

支持快速插入/删除

deque

Double-ended queue

双端队列

Sequential Container Adaptors

顺序容器适配器

stack

Last in/First out stack

后进先出(LIFO)堆栈

queue

First in/First out queue

先进先出(FIFO)队列

priority_queue

Priority-managed queue

有优先级管理的队列


Team LiB
Previous Section Next Section