Quote:Original post by darenking
The first one is as easy as vector pie. I like it!
How come you don't need iterators?
Iterators are designed to just look like fancy pointers, and vectors to look like fancy arrays. You could do this:
const char array[] = "I like pie oh yes I do";const char * begin = array;const char * end = array + size_of( array ); //note: my own size_of function, not the operator sizeof, although here they're interchangeablefor ( const char * i = begin ; i != end ; ++i ) { putchar( *i );}
Which is the same concept as this:
std::vector< char > array = ...;std::vector< char >::iterator begin = array.begin();std::vector< char >::iterator end = array.end();for ( std::vector< char >::iterator i = begin ; i != end ; ++i ) { putchar( *i );}
Similarly, one could do this:
const char array[] = "I like pie oh yes I do";for ( int i = 0 ; i < size_of( array ) ; ++i ) { putchar( array );}
Or for the same effect, this:
std::vector< char > array = ...;for ( int i = 0 ; i < array.size() ; ++i ) { putchar( array );}
Quote:I guess you need iterators for more complex things like deleting one from the middle.
Yes and no. If I wanted to remove the third element of a vector, I could do:
my_vector.erase( my_vector.begin() + 2 );
(since .begin() + 0 is the same as .begin() which is the first element). I say yes and no because yes, this example uses iterators - but no, because you never actually have a named iterator variable :-).