Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: "NULL" iterator

Reply
Thread Tools

Re: "NULL" iterator

 
 
Nick Hounsome
Guest
Posts: n/a
 
      04-01-2004

"Dave" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.


 
Reply With Quote
 
 
 
 
Dave
Guest
Posts: n/a
 
      04-01-2004

"Nick Hounsome" <(E-Mail Removed)> wrote in message
news:R5Xac.7522$(E-Mail Removed)...
>
> "Dave" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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...


 
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
List iterator assignment fails, assert iterator not dereferencable David Bilsby C++ 5 10-09-2007 02:05 PM
What makes an iterator an iterator? Steven D'Aprano Python 28 04-20-2007 03:34 AM
Difference between Java iterator and iterator in Gang of Four Hendrik Maryns Java 18 12-22-2005 05:14 AM
How to convert from std::list<T*>::iterator to std::list<const T*>::iterator? PengYu.UT@gmail.com C++ 6 10-30-2005 03:31 AM
Iterator doubts, Decision on Iterator usage greg C++ 6 07-17-2003 01:26 PM



Advertisments