// A vector is a data structure that holds data in list. It is used like an array
// but has range checking and can be resized. All elements of the vector
// must be the same data type. This program gives examples of filling and
// outputting a vector. Experiment with the different numbers in the code,
// making sure you understand
#include <iostream>
#include <iomanip>
#include <vector>
//prototypes
using namespace std;
void fill (vector<int>& v);
void display (vector<int> & v);
int main()
{
int length=10;
vector<int> list(length,0); // Defines a vector 'list' of integers and initializes each element to 0
fill(list);
display(list);
return 0;
}
void display (vector<int> & v) // The '&' denotes that v is a reference parameter.
{ // It saves memory to pass a parameter by reference
int i;
for ( i=0;i<v.size();i++) // size() is a public function that returns the number of elements
{
cout<<setw(5)<<v[i]; // Experiment by changing the 5 to different numbers.
} // What do you think 'setw' stands for?
cout<<endl<<endl;
}
void fill (vector<int> & v) // Here again v is a reference parameter. v will be filled, and
{ // therefore, changed by this function.
int i;
for ( i=0;i<v.size();i++)
{
v[i] = i;
}