Table of Contents
A vector is similar to an array, except that it can "grow" to accommodate as many items as you actually try to put into it:
A very useful structure.
We'll use it to implement many other ADTs.
Your text introduces a somewhat simplified version (the
minivector) of the
We'll use that simplified form when we look at how to implement
vectors, but will start by studying the
interface so that you know what you can do when programming
applications of it.
First, however, a slight digression into C++-isms. I stated earlier that “A vector is similar to an array”. The similarity comes from the fact that vectors keep their elements in numeric order and allow you to access them via indexing:
int a; vector<int> v; ⋮ a[i] = v[i+1];
string b; vector<string> w; ⋮ w = "Hello";
Folderol c; vector<Folderol> x; ⋮ x = c;
Because vectors are templates, we can have vectors containing almost any other type.
Why “almost any type”? You might remember that not every type can be used in an array. A type can only be used as array elements if that type provides a default constructor, because that constructor is used to initialize the elements of the array.
Similarly, types that are to be used as vector elements have a special
requirement as well. This requirement is different from, but no more
stringent than the requirement for arrays. Types to be used in a vector must
supply a copy constructor. The textbook's
vector actually goes beyond this, requiring a
default constructor and an assignment operator as well, but these are not
required in standard versions.