Section 10.2. Associative Containers


10.2. Associative Containers

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.

The operations common to sequential and associative containers are:

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

          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).

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

  • 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.

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

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

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

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.




C++ Primer
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2006
Pages: 223
Authors: Stephen Prata

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net