Vectors in STL


As discussed previously, a vector is one of the containers in the STL. The vector permits the storage of data like an array but is more efficient than an array since it permits dynamic expansion of the array to make room for additional data to be stored into the vector should the need arise. The elements of an array may be accessed with the [ ] operator or with pointers while the elements of a vector may be accessed with the [ ] operator or with iterators.

Not all data may be used with a vector. In order for an ADT to work with a vector, the ADT must support the operators:

image from book

 ==, <, >, <= and >= 

image from book

What you should do is to check your compiler to be sure which of these operators are supported.

In order to use vectors, you must include the following header:

image from book

 #include<vector> 

image from book

The container vector is in fact a template class. Since vector is a template class, we would define instances as we did with other template classes by specifying the data type like the following statements:

image from book

 vector<short> v1; vector<double> v2; vector<Date> v3; 

image from book

Actually the class vector has several constructors as described in the following table that shows their prototypes:

image from book

Open table as spreadsheet

Constructor

Purpose

vector( );

Creates an empty vector object of the specified data type.

vector(long n);

Creates a vector object of the specified data type with capacity for n elements.

vector(long n, datatype val);

Creates a vector object of the specified data:datatype with n copies of the data element val of type datatype

vector(const vector &v);

Creates a vector object of the specified data type with a copy of all of the elements in the vector v.

vector(datatype* firstptr, datatype* lastptr)

Creates a vector object of the specified data type that contains copies of elements in the memory locations from pointer: firstptr up to pointer: lastptr.

image from book

Using these constructors, we could have definitions of vectors like the following:

image from book

 vector<short> v1;                    // creates an empty short vector v1 vector<double> v2(20);           // creates a double vector v2 with                                                                                                                                                                                                                                                                                                                                                          // a capacity for 20 doubles. vector<long> v3(5,3443);        // creates a long vector v3 with 5                                                                                                                                                                                                                                                                                                  // copies of 3443. vector<long> v5(v3);             // creates a long vector v5 that                                                                                                                                                                                                                                                                                                  // contains a copy                                                                                                                                                                                                                                                                                                  // of all of the data in v3. vector<char> v4(ptr,ptr+4);     // creates a char vector v4 whose                                                                                                                                                                                                                                                                                                  // elements are                                                                                                                                                                                                                                                                                                  // the chars beginning at the address                                                                                                                                                                                                                                                                                                  // contained in the pointer ptr and                                                                                                                                                                                                                                                                                                  // going to the address contained                                                                                                                                                                                                                                                                                                  // in the pointer ptr + 4. 

image from book

The template vector supports the following operators (of course the ADT that is the data type must support these operators as well):

image from book

Open table as spreadsheet

Operator

Purpose

==( )

Compares data in the two vectors and returns a boolean.

<( )

Compares data in the two vectors and returns a boolean.

>( )

Compares data in the two vectors and returns a boolean.

!=( )

Compares data in the two vectors and returns a boolean.

<=( )

Compares data in the two vectors and returns a boolean.

>=( )

Compares data in the two vectors and returns a boolean.

[ ]

Permits access to the elements of the vector object.

image from book

Some of the other vector member template functions are listed in the following table (this is not a complete list of the member functions and you should check your compiler to determine which ones it supports):

image from book

Open table as spreadsheet

Member function

Purpose

at(i)

Returns the ith value of the vector object with range checking where it would throw an out-of-range exception if there is not an ith value.

capacity( )

Returns the current number of elements that the vector object can contain without growing. i.e. without moving the vector object to a new location

size( )

Returns the current number of elements that the vector object contains.

reserve(n)

Increases the capacity of the vector object to n (but not the size, i.e. does not increase the number of elements currently in the vector object just the capacity).

empty( )

Returns true if the vector object is empty and false if it contains any elements.

push_back(val)

Adds val to the end of the vector object and where val is of the required data type for the vector object.

pop_back( )

Removes the value at end of the vector object.

front( )

Accesses the front element of the vector object.

back( )

Accesses the back element of the vector object.

swap( )

Swaps the elements of two different vectors e.g. where v1.swap(v2); would swap the elements of v1 for those of v2 and visa versa.

v[i]

Is the ith element of the vector object v without range checking i.e. whether there is an ith element in the vector object.

=( )

Is the assignment operator where e.g. you could have a statement like: v1 = v2;

image from book

Some examples of vector objects and their member functions are the following: vector1.cpp, vector2.cpp, vector3.cpp, and vector4.cpp (uses date.h). As you can see from these examples and the overall description of vectors, in most cases they are a better container than the C++ array.

You may access an element of a vector by using the [ ] operator or by using an iterator. To create an iterator for a particular vector you must use a definition like the following:

image from book

 vector<datatype>::iterator p; 

image from book

Then you would connect the iterator p to a vector defined like the following:

image from book

 vector<datatype> v1; 

image from book

by using a statement like the following:

image from book

 P = v1.begin(); 

image from book

To access the elements of the vector using the iterator, you must use the dereference operator (like what is done with pointers) as in the following:

image from book

 cout << *p; 

image from book

For example using iterators see vector5.cpp. (Note that this example requires the header: date.h)) What you will notice in this example is that an iterator has properties similar to a pointer. For example an iterator can use some of the arithmetic operators as is done with pointers like the following:

image from book

 --( ), ++( ), ( )--, ( )++, += and -= 

image from book

To interact with vectors, there are also special vector member functions which return iterators. For example begin( ) returns an iterator at the start of the vector and end( ) returns an iterator to the end of the vector.




Intermediate Business Programming with C++
Intermediate Business Programming with C++
ISBN: 738453099
EAN: N/A
Year: 2007
Pages: 142

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