By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,407 Members | 920 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,407 IT Pros & Developers. It's quick & easy.

How to convert between iterators and pointers?

P: n/a
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.
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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" <mr**********@yahoo.com> wrote in message
news:8d**************************@posting.google.c om...
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.

Jul 22 '05 #2

P: n/a
Leon wrote in news:8d**************************@posting.google.c om:
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/
Jul 22 '05 #3

P: n/a
Leon wrote:
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

Jul 22 '05 #4

P: n/a
In article <8d**************************@posting.google.com >,
mr**********@yahoo.com says...

[ ... ]
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.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.