Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to convert between iterators and pointers?

Reply
Thread Tools

How to convert between iterators and pointers?

 
 
Leon
Guest
Posts: n/a
 
      12-20-2003
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.
 
Reply With Quote
 
 
 
 
dwrayment
Guest
Posts: n/a
 
      12-20-2003
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" <> wrote in message
news: 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.



 
Reply With Quote
 
 
 
 
Rob Williscroft
Guest
Posts: n/a
 
      12-20-2003
Leon wrote in news: 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/
 
Reply With Quote
 
Jeff Schwab
Guest
Posts: n/a
 
      12-20-2003
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

 
Reply With Quote
 
Jerry Coffin
Guest
Posts: n/a
 
      12-20-2003
In article < >,
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
plain iterators and reverse iterators on vector subramanian100in@yahoo.com, India C++ 10 08-08-2009 08:28 AM
relation between std::iterator and other container iterators lovecreatesbea...@gmail.com C++ 1 02-06-2008 10:34 AM
distance between stream iterators fastback66@cox.net C++ 4 11-15-2005 01:45 AM
Iterators and reverse iterators Marcin Kaliciński C++ 1 05-08-2005 09:58 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57