Re: How to convert between iterators and pointers?
From: dwrayment (dwrayment_at_rogers.com)
Date: Sat, 20 Dec 2003 12:13:52 GMT
well first i cant for the life of me see why youd want to assign a point to
an iterator. my suggestion is to create a vector of Vertex pointers.
aka std::vector<Vertex *> vertices.
then stl automatically assigns iterators. ex *(vertices.begin()) =
Vertex * and all iterator functions apply.
"Leon" <firstname.lastname@example.org> wrote in message
> Hi all.
> 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 v;
> 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,