On 2007-04-24 19:01, desktop wrote:
In accelerated C++ on page 146 there is this example:
template <class In, class Out>
Out copy(In begin, In end, Out dest)
{
While (begin != end)
*dest++ = *begin++;
return dest;
}
They say that the function takes 3 iterators, but is that not just
another name for a pointer?
If we are dealing with a string begin would be a pointer to the first
char, end would be a pointer to the last char and dest would contain
the copy.
Or does iterator mean something more complicated?
An iterator is a concept in C++, anything that fulfills the requirements
can be used as iterators. In most (all?) cases a simple pointer does
fulfill the requirements of an iterator and can thus be use where an
iterator is required.
But an iterator can be so much more, consider for example std::map,
which is usually implemented as a RB-tree, std::map has a number of
methods that returns iterators, you can as an example iterate through
all elements in the map, this you can not do with a pointer since it
would require that all elements were contiguously laid out in memory.
The same goes for std::list which is a double-linked list.
To take the example with a string (a C++ one, and not a C char array)
there is no guarantee that the second char in the string is on the
memory-location after the first, but using iterators we let them worry
about that and just increment it to get an iterator to the next character.