Team LiB
Previous Section Next Section

10.2. Associative Containers

10.2. 关联容器

Associative containers share many, but not all, of the operations on sequential containers. Associative containers do not have the front, push_front, pop_front, back, push_back, or pop_back operations.

关联容器共享大部分——但并非全部——的顺序容器操作。关联容器不提供 frontpush_frontpop_frontbackpush_back 以及 pop_back 操作。

The operations common to sequential and associative containers are:

顺序容器和关联容器公共的操作包括下面的几种:

  • The first three constructors described in Table 9.2 (p. 307):

    表 9.2 描述的前三种构造函数:

         C<T> c;          // creates an empty container
         // c2 must be same type as c1
         C<T> c1(c2);    // copies elements from c2 into c1
         // b and e are iterators denoting a sequence
         C<T> c(b, e);   // copies elements from the sequence into c
    

    Associative containers cannot be defined from a size, because there would be no way to know what values to give the keys.

    关联容器不能通过容器大小来定义,因为这样的话就无法知道键所对应的值是什么。

  • The relational operations described in Section 9.3.4 (p. 321).

    第 9.3.4 节中描述的关系运算。

  • The begin, end, rbegin, and rend operations of Table 9.6 (p. 317).

    表 9.6 列出的 beginendrbeginrend 操作。

  • The typedefs listed in Table 9.5 (p. 316). Note that for map, the value_type is not the same as the element type. Instead, value_type is a pair representing the types of the keys and associated values. Section 10.3.2 (p. 361) explains the typedefs for maps in more detail.

    表 9.5 列出的类型别名(typedef)。注意,对于 map 容器,value_type 并非元素的类型,而是描述键及其关联值类型的 pair 类型。第 10.3.2 节 将详细解释 map 中的类型别名。

  • The swap and assignment operator described in Table 9.11 (p. 329). Associative containers do not provide the assign functions.

    表 9.11 中描述的 swap 和赋值操作。但关联容器不提供 assign 函数。

  • The clear and erase operations from Table 9.10 (p. 326), except that the erase operation on an associative container returns void.

    表 9.10 列出的 clearerase 操作,但关联容器的 erase 运算返回 void 类型。

  • The size operations in Table 9.8 (p. 324) except for resize, which we cannot use on an associative container.

    表 9.8 列出的关于容器大小的操作。但 resize 函数不能用于关联容器。

Elements Are Ordered by Key

根据键排列元素

The associative container types define additional operations beyond the ones just listed. They also redefine the meaning or return type of operations that are in common with the sequential containers. The differences in these common operations reflect the use of keys in the associative containers.

除了上述列出的操作之外,关联容器还提供了其他的操作。而对于顺序容器也提供的相同操作,关联容器也重新定义了这些操作的含义或返回类型,其中的差别在于关联容器中使用了键。

There is one important consequence of the fact that elements are ordered by key: When we iterate across an associative container, we are guaranteed that the elements are accessed in key order, irrespective of the order in which the elements were placed in the container.

“容器元素根据键的次序排列”这一事实就是一个重要的结论:在迭代遍历关联容器时,我们可确保按键的顺序的访问元素,而与元素在容器中的存放位置完全无关。



Exercises Section 10.2

Exercise 10.3:

Describe the differences between an associative container and a sequential container.

描述关联容器和顺序容器的差别。

Exercise 10.4:

Give illustrations on when a list, vector, deque, map, and set might be most useful.

举例说明 listvectordequemap 以及 set 类型分别适用的情况。


Team LiB
Previous Section Next Section