This question has had me for a while: Why use iterators when you can access the elements directly. For example, suppose we want to fill an empty vector with a for loop. Why would we do it like this:
Code:
vector< int > v( 100 );
for ( vector< int >::iterator it = v.begin(); it != v.end(); ++it )
{
*it = 0;
}
...when you can do it like this:
Code:
vector< int > v( 100 );
for ( int i = 0; i < v.size(); ++i )
{
v[i] = 0;
}
To me, the latter is much easier to read, much easier to type, just as optimised and just as safe. Another example is using a function that requires an iterator. For the erase() function, you can simply do this:
Code:
v.erase( b.begin() + i );
Obviously I'm missing something here, so can someone point it out to me?
Thanks.