Re: "NULL" iterator

Discussion in 'C++' started by Nick Hounsome, Apr 1, 2004.

  1. "Dave" <> wrote in message
    news:...
    > Hello all,
    >
    > Suppose I am given an iterator into a std::list. Furthermore, I know that
    > elements will never be removed from the list, so I know that as long as

    the
    > iterator comes in valid, it will always be valid.
    >
    > But here's the kicker: I need the ability for this iterator to refer to no
    > element as well as to valid elements. This is akin to the value NULL for
    > pointers. list::end() won't work because it changes with time.
    >
    > Can anybody think of a way in which I might implement this?


    Why are you trying to do something that doesn't work when you know perfectly
    well what does?
    Use pointers instead of iterators!
    If the elements are never removed from the list then pointers to them will
    not change.
    Nick Hounsome, Apr 1, 2004
    #1
    1. Advertising

  2. Nick Hounsome

    Dave Guest

    "Nick Hounsome" <> wrote in message
    news:R5Xac.7522$...
    >
    > "Dave" <> wrote in message
    > news:...
    > > Hello all,
    > >
    > > Suppose I am given an iterator into a std::list. Furthermore, I know

    that
    > > elements will never be removed from the list, so I know that as long as

    > the
    > > iterator comes in valid, it will always be valid.
    > >
    > > But here's the kicker: I need the ability for this iterator to refer to

    no
    > > element as well as to valid elements. This is akin to the value NULL

    for
    > > pointers. list::end() won't work because it changes with time.
    > >
    > > Can anybody think of a way in which I might implement this?

    >
    > Why are you trying to do something that doesn't work when you know

    perfectly
    > well what does?
    > Use pointers instead of iterators!
    > If the elements are never removed from the list then pointers to them will
    > not change.
    >
    >

    So, let's see...

    struct foo_t{...};
    list<foo_t> my_list;

    // Put elements in the list

    list<foo_t>::iterator iter;
    // Set iter to refer to some element in the list

    foo_t *ptr = &(*iter);

    I guess it is true to say that this does yield the actual address of the
    element. If I use the pointer in a read-only matter, I suppose there is
    nothing wrong with doing this. If anybody does see a problem, please let me
    know...
    Dave, Apr 1, 2004
    #2
    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,393
  2. Replies:
    5
    Views:
    26,428
    Mike Schilling
    Mar 29, 2006
  3. greg
    Replies:
    6
    Views:
    441
    Dietmar Kuehl
    Jul 17, 2003
  4. Replies:
    6
    Views:
    620
    Jim Langston
    Oct 30, 2005
  5. Steven D'Aprano

    What makes an iterator an iterator?

    Steven D'Aprano, Apr 18, 2007, in forum: Python
    Replies:
    28
    Views:
    1,119
    Steven D'Aprano
    Apr 20, 2007
Loading...

Share This Page