There is a PDF version [ vectors.pdf ] suitable for an ereader.
Vectors are held in a special library and can be used in a file that has
#include <vector>at its beginning. It uses the "std" namespace so you will need "using ...;" statements.
| Declaration | vector<type> v(initial_size); |
| Accessors | v.empty(), v.size(), v.front(), v.back() |
| Mutators | v.push_back(T), v.pop_back() |
| Operators | v[int], v.at(int), v1=v2;, v1==v2 |
vector<T> v;declares a new and empty vector called v. Given object v declare like the above you can do the following things with it:
v.empty()
v.size()
v.push_back(t)
v.pop_back()
v[i]
v = v1
#include <iostream>
#include <vector>
using namespace std;
void print( const vector<int>& ) ;//utility function outputs a vector of ints
void print_backwards( const vector<int> &);Then we describe the main program:
int main()
{
vector<int> v;
int number;
cout <<"Input some numbers and then end the input\n";
while(cin>>number){
v.push_back(number);
}//while(more)
print(v);
print_backwards(v);
}//mainFinally the two procedures that print out the data:
void print_backwards( const vector<int> &a)
{
for(int i=a.size()-1; i>=0; --i)
cout << a[i] << " ";
cout << endl;
cout << "----------------"<<endl;
}//print_backwardsand
void print( const vector<int>& a)
{
for(int i=0; i<a.size(); ++i)
cout << a[i] << " ";
cout << endl;
cout << "----------------"<<endl;
const int MAX = 10;
float a[MAX];
...
for(int i=0; i<MAX; ++i)
process(a[i]);
...Arrays are like vectors except that:
You must be sure that all indices or subscripts stay between 0 and size()-1 inclusive.
Trying to change the size of an array does not work.
If a program compiles, loads, crashes, and there is a subscript operator ([...]) then check to see if the subscripted item has been put into the container before the subscripted element is accessed. A very common error is to declare a vector with no length and to use [...] as if it created new elements. It doesn't. Use 'push_back(..)' to add new elements to a vector.
If you are unable to show that a subscript is in range then use a condition like the following to guard thing[i] from errors:
0<= i && i < thing.size()
If a program compiles, loads and crashes and it has a pop_back, function than make sure that there is an item to be "popped"! In code you can use the empty function to guard statements that contain "pop" from blowing up.
On some older compilers and libraries when you need a <string> as well as <vector> you need to
#include <string>before including the list or vector rather in the reverse order! Older string library appear to define some special versions of vector and list operators and the older compilers can not make up its mind which to use.
If the standard <vector> is not found then you are using an older C++ compiler.
You can have vectors of vectors:
vector < vector <int> > matrix;but you must put at least one space between the two ">" symbols. The following is wrong:
vector < vector <int>> matrix;
. . . . . . . . . ( end of section Errors) <<Contents | End>>
. . . . . . . . . ( end of section Introducing C++ Vectors and Arrays) <<Contents | End>>