Deitel & Associates, Inc. Logo

Back to
digg.png delicious.png blinkit.png furl.png
C++ How to Program, 5/e

© 2005
pages: 1500
Buy the Book!
Amazon logo
InformIT logo

23.2.1 vector Sequence Container (continued)

Lines 46 and 51 use the two erase functions that are available in all first-class containers. Line 46 indicates that the element at the location specified by the iterator argument should be removed from the container (in this example, the element at the beginning of the vector). Line 51 specifies that all elements in the range starting with the location of the first argument up to—but not including—the location of the second argument should be erased from the container. In this example, all the elements are erased from the vector. Line 53 uses function empty (available for all containers and adapters) to confirm that the vector is empty.

Common Programming Error
Common Programming Error 23.4
Erasing an element that contains a pointer to a dynamically allocated object does not delete that object; this can lead to a memory leak.

Line 56 demonstrates the version of function insert that uses the second and third arguments to specify the starting location and ending location in a sequence of values (possibly

STL exception types Description
out_of_range Indicates when subscript is out of range—e.g., when an invalid subscript is specified to vector member function at.
invalid_argument Indicates an invalid argument was passed to a function.
length_error Indicates an attempt to create too long a container, string, etc.
bad_alloc Indicates that an attempt to allocate memory with s (or with an allocator) failed because not enough memory was available.

Fig. 23.16 Some STL exception types.

from another container; in this case, from array of integers array) that should be inserted into the vector. Remember that the ending location specifies the position in the sequence after the last element to be inserted; copying is performed up to—but not including—this location.

Finally, line 61 uses function clear (found in all first-class containers) to empty the vector. This function calls the version of erase used in line 51 to empty the vector.

[Note: Other functions that are common to all containers and common to all sequence containers have not yet been covered. We will cover most of these in the next few sections. We will also cover many functions that are specific to each container.]

Page 1 | 2 | 3 | 4 | 5 | 6

Tutorial Index