How to convert between iterators and pointers?
From: Leon (mrcool031179_at_yahoo.com)
Date: 20 Dec 2003 02:45:15 -0800
I'm a bit confused about the use of STL iterators and pointers, and I was
wondering if maybe you could give me some pointers ;)
Is it possible to convert between iterators and pointers pointing to the
same type, e.g. between "std::set<Vertex>::iterator vi" and "Vertex *" ?
I found a quote in the MSVC STL documentation, saying:
"Remember that an object pointer can always serve as a
random-access iterator. Therefore, it can serve as any category
of iterator, as long as it supports the proper read/write access
to the sequence it designates."
But I can't seem to assign a "Vertex *" to an iterator, like this:
Vertex *pv = &v;
std::set<Vertex>::iterator vi = pv;
Is there a right way of letting an iterator point to the same object as a
Using an iterator as a pointer also doesn't seem to work. For example:
// Edge constructor declaration:
// Edge(Vertex *pv1, Vertex *pv2);
std::set<Vertex>::iterator vi1, vi2;
// [Set vi1 and vi2 here]
Edge e(vi1, vi2); // Won't compile, saying "No user-defined
// conversion operator available"
Instead, I have to resort to:
Edge e(&(*vi1), &(*vi2));
Is this normal, or am I doing something wrong?
Thanks in advance,