std::container::iterator vs std::container::pointer

Discussion in 'C++' started by Vivi Orunitia, Feb 3, 2004.

  1. Hi all,

    I tried looking this up in the sgi docs but it didn't provide any concrete
    answer to what I'm looking for. Basically, is there any difference between
    using ::iterator for a container vs using ::pointer?

    I did a quick experiment and replaced all the ::iterator in my code with
    ::pointer and it seems to work the same. What I'm think is on the surface
    they're the same but perhaps there're some subtle differences beneath the
    surface that I'm unaware of?

    Thanks
     
    Vivi Orunitia, Feb 3, 2004
    #1
    1. Advertising

  2. Vivi Orunitia

    Sharad Kala Guest

    "Vivi Orunitia" <> wrote in message
    news:Xns94841E297AB88Viviblackmagevillage@199.45.49.11...
    > Hi all,
    >
    > I tried looking this up in the sgi docs but it didn't provide any concrete
    > answer to what I'm looking for. Basically, is there any difference between
    > using ::iterator for a container vs using ::pointer?
    >
    > I did a quick experiment and replaced all the ::iterator in my code with
    > ::pointer and it seems to work the same. What I'm think is on the surface
    > they're the same but perhaps there're some subtle differences beneath the
    > surface that I'm unaware of?


    Yes there is a difference.
    For an STL container Cont<T,..>
    the following typedef's are provided

    typedef T value_type;
    typedef T* pointer;
    typedef T& reference;

    Iterator could be some other class or container::pointer.
    In fact anything(class/pointer) that gives the notion of an iterator could be an
    iterator to the container.
    So one can't make assumptions that container::pointer and container::iterator
    are actually same.

    Best wishes,
    Sharad
     
    Sharad Kala, Feb 3, 2004
    #2
    1. Advertising

  3. As far as I can tell an iterator has all the operations of a pointer like
    deferencing *, ->, etc. which makes sense as iterators are suppose to be
    generalizations of pointers.

    I can imagine, however, where some operations of an iterator might not be
    implemented like ++, -- etc. if they're an iterator of a particular
    category like forward iterators, reverse iterators etc.

    "Sharad Kala" <> wrote in
    news:bvnlp8$uu0i2$-berlin.de:

    >
    >
    > Iterator could be some other class or container::pointer.
    > In fact anything(class/pointer) that gives the notion of an iterator
    > could be an iterator to the container.
    > So one can't make assumptions that container::pointer and
    > container::iterator are actually same.
    >
    >


    hmm, so as far as using them is concerned under what situations would
    this difference be important? Like for example, where using ::iterator
    and ::pointer interchangably would cause potential problems etc.

    Thanks for the response :)
     
    Vivi Orunitia, Feb 3, 2004
    #3
  4. On Tue, 03 Feb 2004 08:46:58 +0000, Vivi Orunitia wrote:

    >> Iterator could be some other class or container::pointer.
    >> In fact anything(class/pointer) that gives the notion of an iterator
    >> could be an iterator to the container.
    >> So one can't make assumptions that container::pointer and
    >> container::iterator are actually same.
    >>
    >>

    >
    > hmm, so as far as using them is concerned under what situations would
    > this difference be important? Like for example, where using ::iterator
    > and ::pointer interchangably would cause potential problems etc.


    Take std::list. An iterator there is definately different from a pointer.
    Its operator++ has to know how to chase the linked list, a very different
    operation for just incrementing a pointer. A std::map::iterator also has
    to point at the next element, it does so by stepping through the tree,
    also a very different operation from incrementing a pointer.

    But in general an iterator _is_not_ a pointer. Sometimes iterators may be
    implemented as pointers, but I guess this is only possible for std::vector
    anyhow. Even for std::vector, the implementation is allowed to use some
    class instead of pointers, and indeed some do.

    Anyhow, an iterator is modeled after a pointer, it makes for a convenient
    framework that is very familiar to most programmers. However this modeling
    is on the conceptual level (you can dereference it, increment it, etc),
    not on the implementation level. That is the beauty of iterators, you can
    just increment it to point at the next element, without having to worry
    about all the magic that the iterator needs to do to find the next element.

    HTH,
    M4
     
    Martijn Lievaart, Feb 3, 2004
    #4
  5. Vivi Orunitia

    Sharad Kala Guest

    "Vivi Orunitia" <> wrote in message
    news:Xns948426AD92D26Viviblackmagevillage@199.45.49.11...
    > As far as I can tell an iterator has all the operations of a pointer like
    > deferencing *, ->, etc. which makes sense as iterators are suppose to be
    > generalizations of pointers.
    >
    > I can imagine, however, where some operations of an iterator might not be
    > implemented like ++, -- etc. if they're an iterator of a particular
    > category like forward iterators, reverse iterators etc.
    >
    > "Sharad Kala" <> wrote in
    > news:bvnlp8$uu0i2$-berlin.de:
    >
    > >
    > >
    > > Iterator could be some other class or container::pointer.
    > > In fact anything(class/pointer) that gives the notion of an iterator
    > > could be an iterator to the container.
    > > So one can't make assumptions that container::pointer and
    > > container::iterator are actually same.
    > >
    > >

    >
    > hmm, so as far as using them is concerned under what situations would
    > this difference be important? Like for example, where using ::iterator
    > and ::pointer interchangably would cause potential problems etc.


    Probably you are using vectors and iterators are pointers on your
    implementation.
    Try using a list and see even if the code compiles!

    #include<list>
    #include<iostream>
    using namespace std;

    int main(){
    typedef list<int> IntList;
    IntList li;
    li.push_back(5);
    li.push_back(7);

    IntList::iterator itr = li.begin();
    // IntList::pointer p = li.begin(); //ERROR

    IntList::pointer p = &(*itr);
    cout << *(++itr); // prints 7
    cout << *(++p); // god knows, lucky if you get a crash here

    }
     
    Sharad Kala, Feb 3, 2004
    #5
  6. "Vivi Orunitia" <> wrote in message
    news:Xns94841E297AB88Viviblackmagevillage@199.45.49.11...
    > Hi all,
    >
    > I tried looking this up in the sgi docs but it didn't provide any concrete
    > answer to what I'm looking for. Basically, is there any difference between
    > using ::iterator for a container vs using ::pointer?
    >
    > I did a quick experiment and replaced all the ::iterator in my code with
    > ::pointer and it seems to work the same. What I'm think is on the surface
    > they're the same but perhaps there're some subtle differences beneath the
    > surface that I'm unaware of?
    >
    > Thanks


    You probably did your testing with std::vector or std::string. Pointers and
    iterators are likely to be the same for these classes, they are very
    unlikely to be the same for other classes.

    John
     
    John Harrison, Feb 3, 2004
    #6
  7. Vivi Orunitia

    Chris Theis Guest

    "Vivi Orunitia" <> wrote in message
    news:Xns948426AD92D26Viviblackmagevillage@199.45.49.11...
    > As far as I can tell an iterator has all the operations of a pointer like
    > deferencing *, ->, etc. which makes sense as iterators are suppose to be
    > generalizations of pointers.
    >
    > I can imagine, however, where some operations of an iterator might not be
    > implemented like ++, -- etc. if they're an iterator of a particular
    > category like forward iterators, reverse iterators etc.
    >
    > "Sharad Kala" <> wrote in
    > news:bvnlp8$uu0i2$-berlin.de:
    >
    > >
    > >
    > > Iterator could be some other class or container::pointer.
    > > In fact anything(class/pointer) that gives the notion of an iterator
    > > could be an iterator to the container.
    > > So one can't make assumptions that container::pointer and
    > > container::iterator are actually same.
    > >
    > >

    >
    > hmm, so as far as using them is concerned under what situations would
    > this difference be important? Like for example, where using ::iterator
    > and ::pointer interchangably would cause potential problems etc.
    >
    > Thanks for the response :)


    As Sharad already pointed out iterators might be implemented in terms of
    pointers (often found for vector container implementations). However, you
    should not rely on this! For example the standard signature for the begin
    function of a container is to return an iterator.
    You might find implementations where the following line is valid:

    vector<char> Data;
    // fill vector
    printf("%s", Data.begin() );

    This might work but it's certainly NOT portable!! Hence, it's always safe to
    stick to the iterator definition no matter which terms the implementation is
    done in.

    Regards
    Chris
     
    Chris Theis, Feb 3, 2004
    #7
  8. Vivi Orunitia

    Sharad Kala Guest


    > You probably did your testing with std::vector or std::string.


    Yes and additionally std::vector or std::string on his *implementation* used
    pointers as iterators.
    There are some vector implementations that do not use pointers as iterators.
    Additionally code like vec.begin()++ would be invalid in case of pointers.
     
    Sharad Kala, Feb 3, 2004
    #8
  9. Vivi Orunitia

    tom_usenet Guest

    On Tue, 03 Feb 2004 08:46:58 GMT, Vivi Orunitia
    <> wrote:

    >As far as I can tell an iterator has all the operations of a pointer like
    >deferencing *, ->, etc. which makes sense as iterators are suppose to be
    >generalizations of pointers.


    Right, a pointer is a model of a random access iterator. That is, a
    pointer is a type of iterator.

    >I can imagine, however, where some operations of an iterator might not be
    >implemented like ++, -- etc. if they're an iterator of a particular
    >category like forward iterators, reverse iterators etc.


    Right - non-bidirectional iterators don't provide --.

    >"Sharad Kala" <> wrote in
    >news:bvnlp8$uu0i2$-berlin.de:
    >
    >>
    >>
    >> Iterator could be some other class or container::pointer.
    >> In fact anything(class/pointer) that gives the notion of an iterator
    >> could be an iterator to the container.
    >> So one can't make assumptions that container::pointer and
    >> container::iterator are actually same.
    >>
    >>

    >
    >hmm, so as far as using them is concerned under what situations would
    >this difference be important? Like for example, where using ::iterator
    >and ::pointer interchangably would cause potential problems etc.


    When ::iterator isn't the same as ::pointer - you'll get compiler
    errors. There is only one situation where they might be the same -
    using std::vector with certain (mostly old) standard library
    implementations. Most modern libraries have a class type for
    vector::iterator. You can convert an iterator into a pointer but not
    vice versa. e.g.

    int* p = &*mylist.begin();

    Tom

    C++ FAQ: http://www.parashift.com/c -faq-lite/
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
     
    tom_usenet, Feb 3, 2004
    #9
  10. Vivi Orunitia wrote:

    > Hi all,
    >
    > I tried looking this up in the sgi docs but it didn't provide any concrete
    > answer to what I'm looking for. Basically, is there any difference between
    > using ::iterator for a container vs using ::pointer?
    >
    > I did a quick experiment and replaced all the ::iterator in my code with
    > ::pointer and it seems to work the same. What I'm think is on the surface
    > they're the same but perhaps there're some subtle differences beneath the
    > surface that I'm unaware of?
    >
    > Thanks


    A good example of the difference is when using a binary tree
    container, or a linked list. To point to the next element
    in an collinear sequence, one can use a pointer and just
    increment the pointer. However, with a linked list or
    tree, the elements may not be adjacient to each other,
    so a plain increment on a pointer will not work.

    Iterators allow one to write a function to point to
    the next element in the contain. So for a singly
    linked list, the operator++ may be:
    {
    return next_link;
    }
    For other data structures, the operation may be
    more complex.

    Many iterator implementations will try their best to
    masquarade as a pointer; which helps out with accessing
    elements in a container.

    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.raos.demon.uk/acllc-c /faq.html
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
     
    Thomas Matthews, Feb 3, 2004
    #10
  11. Thanks for all the response guys. I think the distinction is starting to
    become clearer for me now :)

    I always went with ::iterator for the std containers I use as far as I can
    remember but I was looking up the std reference the other day and noticed
    they also had ::pointer. So it just got me asking some questions like what
    their difference were and if ::iterators existed why do we have ::pointers
    and questions like that.

    Thanks
     
    Vivi Orunitia, Feb 3, 2004
    #11
  12. On Tue, 03 Feb 2004 18:43:03 +0000, Vivi Orunitia wrote:

    > Thanks for all the response guys. I think the distinction is starting to
    > become clearer for me now :)
    >
    > I always went with ::iterator for the std containers I use as far as I can
    > remember but I was looking up the std reference the other day and noticed
    > they also had ::pointer. So it just got me asking some questions like what
    > their difference were and if ::iterators existed why do we have ::pointers
    > and questions like that.


    The thing I think no one mentioned up to now, is the idea behind
    container::pointer. This could conceivably be different from a real
    pointer for a container that "proxies" for some underlying architecture.
    In that case, the pointer type would be a proxy class that implemented
    pointer-like behaviour (just like an iterator, but slightly different).

    Unfortunately, this would not be a STL container, it was a nice experiment
    that failed in the end. The details escape me at the moment (where /are/
    all my Meyers books gone), I'm sure someone will fill this in, but for
    anything in the standard library, except vector<bool> you can assume
    container<T>::pointer is a T*. But using container<T>::pointer (and
    reference!) in your own generic code where appropriate makes your
    algorithm well behaved with types that implement such a proxy-pointer-type.

    HTH,
    M4
     
    Martijn Lievaart, Feb 4, 2004
    #12
    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. Hendrik Maryns
    Replies:
    18
    Views:
    1,458
  2. Steven T. Hatton
    Replies:
    4
    Views:
    3,962
    Rob Williscroft
    Dec 5, 2004
  3. forester
    Replies:
    16
    Views:
    728
    Howard Hinnant
    Aug 25, 2005
  4. Replies:
    6
    Views:
    683
    Jim Langston
    Oct 30, 2005
  5. Replies:
    1
    Views:
    384
    Michael DOUBEZ
    Feb 6, 2008
Loading...

Share This Page