The vector data abstraction, Include files – HP Integrity NonStop H-Series User Manual

Page 55

Advertising
background image

Click on the banner to return to the user guide home page.

©Copyright 1996 Rogue Wave Software

The vector Data Abstraction

The

vector

container class generalizes the concept of an ordinary C array. Like an array, a

vector is an indexed data structure, with index values that range from 0 to one less than the
number of elements contained in the structure. Also like an array, values are most commonly
assigned to and extracted from the vector using the subscript operator. However, the vector
differs from an array in the following important respects:

A vector has more "self-knowledge" than an ordinary array. In particular, a vector can be
queried about its size, about the number of elements it can potentially hold (which may
be different from its current size), and so on.

The size of the vector can change dynamically. New elements can be inserted on to the
end of a vector, or into the middle. Storage management is handled efficiently and
automatically. It is important to note, however, that while these abilities are provided,
insertion into the middle of a vector is not as efficient as insertion into the middle of a

list

(

Chapter 6:

Placing Elements into a List

). If many insertion operations are to be performed,

the list container should be used instead of the

vector

data type.

The

vector

container class in the standard library should be compared and contrasted to the

deque

container class we will describe in more detail in

Chapter 7

. Like a vector, a deque

(pronounced "deck") is an indexed data structure. The major difference between the two is that
a deque provides efficient insertion at either the beginning or the end of the container, while a
vector provides efficient insertion only at the end. In many situations, either structure can be
used. Use of a vector generally results in a smaller executable file, while, depending upon the
particular set of operations being performed, use of a deque may result in a slightly faster
program.

Include Files

Whenever you use a

vector

, you must include the vector header file.

# include <vector>

Advertising
This manual is related to the following products: