![[Dept of Engineering]](http://www.eng.cam.ac.uk/images/house_style/engban-s.gif)
vector <int> v; // an empty integer vector
vector <int> v(5); // 5 elements, initialised to the default
// value of the elements, in this case 0.
// Vector resizing is expensive, so set to
// the max size if you can.
vector <int> v(5,13); // 5 elements initialised to 13
vector <int> w(v.begin(),v.end()); // initialised from another container.
Many routines are available for vectors. For example, if you have a vector of strings, you can remove all the strings beginning with 'p' by doing the following
// for this method you need to sort the items first
sort(fruit.begin(), fruit.end());
// create an iterator p1 for use with a vector of strings
// and set it to point to the first string to remove
vector<string>::iterator p1=
find_if(fruit.begin(), fruit.end(), initial('p'));
// create and set p2 to point to the first string after
// those that begin with 'p'
vector<string>::iterator p2=
find_if(p1, fruit.end(), initial_not('p'));
// Now erase
fruit.erase(p1, p2);
Note that you can't do the following (searching from each end of the vector) because forward and reverse iterators are different types, and you can't use erase with mixed types.
sort(fruit.begin(), fruit.end());
vector<string>::iterator p1=
find_if(fruit.begin(), fruit.end(), initial('p'));
vector<string>::reverse_iterator p2=
find_if(fruit.rbegin(), fruit.rend(), initial('p'));
fruit.erase(p1, p2);
These above examples use the standard algorithms, which are always useful as a fallback option, but the vector class has a more specialised routine which is likely to be faster
fruit.remove_if(initial('p'));
Remember that