How to convert between iterators and pointers?

Discussion in 'C++' started by Leon, Dec 20, 2003.

  1. Leon

    Leon Guest

    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.
    Leon, Dec 20, 2003
    #1
    1. Advertising

  2. Leon

    dwrayment Guest

    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:...
    > 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.
    dwrayment, Dec 20, 2003
    #2
    1. Advertising

  3. Leon wrote in news::

    > 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/
    Rob Williscroft, Dec 20, 2003
    #3
  4. Leon

    Jeff Schwab Guest

    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
    Jeff Schwab, Dec 20, 2003
    #4
  5. Leon

    Jerry Coffin Guest

    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.
    Jerry Coffin, Dec 20, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gianni Mariani

    iterators, pointers and (T*)-1

    Gianni Mariani, Oct 21, 2003, in forum: C++
    Replies:
    11
    Views:
    561
    Gianni Mariani
    Oct 22, 2003
  2. Alex Vinokur
    Replies:
    0
    Views:
    426
    Alex Vinokur
    Jul 26, 2004
  3. Marcin Kaliciñski

    Iterators and reverse iterators

    Marcin Kaliciñski, May 8, 2005, in forum: C++
    Replies:
    1
    Views:
    474
    Kai-Uwe Bux
    May 8, 2005
  4. , India
    Replies:
    10
    Views:
    1,053
    James Kanze
    Aug 8, 2009
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    642
Loading...

Share This Page