How to convert between iterators and pointers?

L

Leon

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.
 
D

dwrayment

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.
 
R

Rob Williscroft

Leon wrote in
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."

All the above is telling you is that a pointer is a random-access
iterator for an inbuilt array (eg int array[20]).

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?

Other than the method you use below, there isn't.
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?

Yes ( Its Normal ).

You could change the declaration of Edge::Edge()

template < typename Iter >
Edge( Iter v1, Iter v2 )
{

}

Note that because a Vertex * is an iterator (*), the above will still
work with pointers.

(*) Though it *isn't* an iterator to a std::set< Vertex >.

Alternitavly, pass by Vertex &, then it becomes Edge e( *vi1, *vi2 ).


Rob. -- http://www.victim-prime.dsl.pipex.com/
 
J

Jeff Schwab

Leon said:
Hi all.

Is it possible to convert between iterators and pointers pointing to the
same type, e.g. between "std::set<Vertex>::iterator vi" and "Vertex *" ?

No, those iterators probably are not pointers.
Is there a right way of letting an iterator point to the same object as a
pointer?

You did it already:
Edge e(&(*vi1), &(*vi2));

Is this normal, or am I doing something wrong?

That's perfectly normal. It's done all the time.

-Jeff
 
J

Jerry Coffin

[ ... ]
Is it possible to convert between iterators and pointers pointing to the
same type, e.g. between "std::set<Vertex>::iterator vi" and "Vertex *" ?

Yes, but it's rarely useful.
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."

This is NOT talking about converting a pointer to an iterator or vice
versa -- it's saying that you can use a pointer AS an iterator, without
doing a conversion at all.
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;

No, and you probably don't want to either.
Is there a right way of letting an iterator point to the same object as a
pointer?
Sure.


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?

This works, but it's rarely what you want to do. Right now, pv1 and pv2
are pointers, but they don't seem to be pointing AT anything. Let's
assume you have something like this:

Vertex v1(/* initizlizers */), v2( /* more initializers */);

then creating an edge from those two vertices requires only:

Edge(&v1, &v2);

because you use use the addresses of the vertices to initialize pointers
that act as iterators. Likewise, if you wanted to initialize a vector
with the contents of an array:

int init[] = {0,1,2,3,4,5,6,7,8,9,10};

std::vector<int> x(init, init+9);

An iterator provides a subset of pointer operations. Exactly what subset
depends on the type of iterator -- a random-access iterator provides
pretty much the full set, while a forward iterator (for example)
provides only quite a limited subset.

A pointer is an iterator (or can be treated as one) but an iterator is
specific to a particular container type, and a pointer is no exception:
a pointer is a random-access iterator for an array or (by special
dispensation) a vector. If you just need something to refer to a single
object, just about any iterator can do the job, and that includes a
pointer.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top