Be aware that when you do this:
std::vector<char> myChars(25, 'v');
You are literally just creating a vector of characters (it is
not a string because it's not null-terminated). If you want a string, look into std string.
Generally, with a vector, do not worry too much about its capacity, unless you have good reason to care about its efficiency. Initially, you should care more about its size() and why you should or should not use a vector vs a list vs a map, etc... You should understand value-semantics and how that affects your vector if it contains objects, POD (plain-old-data), or pointers. You should understand why you can't have a vector of references.
The basic idea behind iterators is this: for a vector, the classical way of iteration is
1 2 3 4 5 6 7 8 9 10 11 12 13
|
#include <vector>
#include <iostream>
using namspace std;
void tryVector() {
vector<int> v;
v.push_back( 5 );
v.push_back( 4 );
unsigned i, n= v.size();
for ( i=0; i<n; ++i )
cout << i << endl;
}
|
Line 11 and 12 is where the iteration happens. STL tries to standardize iterators so that Line 11 looks the same regardless of whether you are dealing with a map, list, or a vector. The iterator will hide that indexing i from you. An iterator will also keep you from reading out-of-bounds memory (if used properly). There are other reasons for iterators such as keeping state and its usefulness in copying and searching, but those things you will pick up as you see more examples.