iterators

C

conrad

I was reading about iterators and how they are
used with containers provided by the STL.
They seem like aliases for pointer types.
Is this the case? What's the benefit of
doing it this way, if so? I'm making a transition
from C to C++. So far, I very much like the idea
of the STL.

Any insight appreciated.
 
V

Victor Bazarov

conrad said:
I was reading about iterators and how they are
used with containers provided by the STL.
They seem like aliases for pointer types.
Is this the case?

No. Iterators are [often] their own types. Pointers,
however, do comply with random access iterators semantics.
What's the benefit of
doing it this way, if so? I'm making a transition
from C to C++. So far, I very much like the idea
of the STL.

Iterators are defined by the requirements imposed on them.
Those are enumerated in the Standard. There are several
categories of iterators. You can read all about those and
more in "C++ Standard Library" by Josuttis.

V
 
I

Ian Collins

conrad said:
I was reading about iterators and how they are
used with containers provided by the STL.
They seem like aliases for pointer types.
Is this the case?

In general, no but in some limited cases (std::vector for example) they
can be, but it is prudent to think of them as their own type.
What's the benefit of
doing it this way, if so? I'm making a transition
from C to C++. So far, I very much like the idea
of the STL.
An iterator can be pointer only where he container's elements are
contiguous in memory. This is not the case for most containers.
Consider std::map, where the elements in the map are typically stored in
a tree, in this case you require a specialised type to iterates through
them.
 
D

David Harmon

On Sun, 29 Jul 2007 01:16:48 -0000 in comp.lang.c++, conrad
I was reading about iterators and how they are
used with containers provided by the STL.
They seem like aliases for pointer types.

They are much like pointer types in the way you use them.
In implementation, they may be actual pointers or something much more
complex, and you the user don't care which. That's the beauty of it.

A std::vector iterator may actually be a pointer. std::set::iterator
less likely. An istream_iterator, even less likely.
 
R

Roland Pibinger

I was reading about iterators and how they are
used with containers provided by the STL.
They seem like aliases for pointer types.

Iterators are abstractions (not aliases) of pointers, containers are
abstractions of C arrays and algorithms are designed as in C (almost,
compare std::sort to qsort).
Is this the case? What's the benefit of
doing it this way, if so?

IMO, the partial success of STL among C++ programmers was due to the
fact that STL picked up familiar C elements and enhanced them with
functional programming concepts.
I'm making a transition
from C to C++. So far, I very much like the idea
of the STL.

Apart from the (for a C programmer) incredibly long and
incomprehensible error messages this should be a smooth transition.
You can largely ignore the 'OO part' (inheritance, virtual functions,
dynamic allocations, exception handling, ...) of the C++ language
because STL doesn't use and support OO. The Koenig & Moo book seems to
be a good introduction into STL programming for people already
proficient in C.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,999
Messages
2,570,244
Members
46,838
Latest member
KandiceChi

Latest Threads

Top