Example of using Vectors
-
The ANSI-ISO C++ Standard Template Library provides a vector class
that can be included into your program using the header file, <vector>.
-
Vectors serve the same purpose as arrays except that they can change size while the program is running.
-
Unlike arrays, however, you cannot (legally) index into a vector, say v[i], to fetch a value or to assign a value unless an element has already been inserted at every index position up to and including index position i.
-
The following declares a variable v for a vector with base type baseType:
vector<baseType> v;
-
Any built-in and user-defined base types are acceptable.
For example, to declare a vector of integer scores:
vector<int> scores;
A vector defined like this one (without a size parameter) is initially empty (zero number of elements).
-
The member function push_back can be used to give a value to the next element of a vector.
The first element starts at position 0.
For example the following gives values to elements 0, 1, and 2 of the vector scores:
int number;
vector<int> scores;
for (int n = 1; n <=3; n++)
{
cout << "Enter a score " << endl;
cin >> number;
scores.push_back(number);
}
-
Vector elements are indexed using the square bracket notation with a starting index of 0 just like arrays.
-
After the above three scores have been given values to the vector scores using the push_back member function,
they can now be changed using the square bracket notation. For example:
scores[2] += 5;
-
The square bracket notation can be used to change the value of an element of a vector
only after it has been given a value, for example using the push_back member function.
-
Another way to give the elements of a vector values is initialization. For example:
vector <float> widths(10);
declares a vector named widths and initializes the first 10 elements to 0.
-
The member function size can be used to determine the number of elements in a vector.
The function size returns a value of type unsigned (an unsigned integer), not a value of type int.
An example of using the size member function:
vector <float> widths(10);
cout << "Enter 10 widths.\n";
for (unsigned n = 0; n < widths.size( ); n++)
cin >> widths[n];
To set the i-th element, for i greater than or equal to 10, you would use push_back.
For example, the following creates an 11th element to widths and sets its value to 8.2:
widths.push_back(8.2)
The size of widths is now 11.
-
If v is a vector and i is greater than or equal to v.size( ),
then assigning a value to v[i] is wrong and dangerous, but may or may not result in an error message (just like the case of arrays).
-
The assignment operator, =, is defined for vectors. If v1 and v2 are vectors, then
v2 = v1;
makes v2 a copy of v1.
-
Another member function, pop_back, removes the last element of a vector, shrinking its size by one.
E.g., to remove the last element of widths and reduce its size by 1:
widths.pop_back( )
Note that the pop-Back function does not return the value that is being removed. If want to know what that element is, you need to capture it first:
double lastValue = widths[widths.size( ) - 1]; // capture the last value first
widths.pop_back( )
-
Vectors can be passed into a function, either as value or reference parameters.
They are usually passed by reference for the sake of efficiency. To protect the vectors from alterations by the function, one can use the const modifier. For example:
void fcn(vector<int>&)
if the function alters the vector, or as
void fcn(const vector<int>&)
if the function is not supposed to alter the vector.