Re: How to convert between iterators and pointers?
From: dwrayment (dwrayment_at_rogers.com)
Date: 12/20/03
- Next message: CoolPint: "Re: Questions about defaut template type argument"
- Previous message: news.hku.hk: "need help for simple c++ programming"
- In reply to: Leon: "How to convert between iterators and pointers?"
- Next in thread: Rob Williscroft: "Re: How to convert between iterators and pointers?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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" <mrcool031179@yahoo.com> wrote in message
news:8d481046.0312200245.6e30235c@posting.google.com...
> 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
> pointer?
>
> 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,
> Leon.
- Next message: CoolPint: "Re: Questions about defaut template type argument"
- Previous message: news.hku.hk: "need help for simple c++ programming"
- In reply to: Leon: "How to convert between iterators and pointers?"
- Next in thread: Rob Williscroft: "Re: How to convert between iterators and pointers?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|