468,496 Members | 1,798 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,496 developers. It's quick & easy.

List Iterator = NULL?

User-Agent: OSXnews 2.081
Xref: number1.nntp.dca.giganews.com comp.lang.c++:818044
Hi,
I am wondering if I can assign a list iterator = NULL. Suppose I have a
class A:
A.h
class A{
//ctors etc etc.
list<A>::iterator it;
}

A.C
//;;
A::some_method
{
it = NULL; /??
}

Currently, I am using a pointer instead of an iterator(Hence above
operation is perfectly valid), but when I wish to delete this particular
object from another list
list<Amylist;

I will have to do a linear time operation. However, if I can use
iterator that can be set to NULL (or some other default identifiable location)
I can avoid this linear time deletionby using "erase" method of list template class.
Or is there some analogous thing to NULL for iterators?

thanks,
amit.

Aug 24 '06 #1
3 22687
Amit Bhatia wrote:
Hi,
I am wondering if I can assign a list iterator = NULL. Suppose I have a
class A:
A.h
class A{
//ctors etc etc.
list<A>::iterator it;
}

A.C
//;;
A::some_method
{
it = NULL; /??
}

Currently, I am using a pointer instead of an iterator(Hence above
operation is perfectly valid), but when I wish to delete this particular
object from another list
list<Amylist;

I will have to do a linear time operation. However, if I can use
iterator that can be set to NULL (or some other default identifiable location)
I can avoid this linear time deletionby using "erase" method of list template class.
Or is there some analogous thing to NULL for iterators?

thanks,
amit.
There is no general NULL for iterators. The analog of a null pointer is
an iterator which is at the end of whatever it's iterating. If you have
a list l in your class, you can set "it" to l.end().

But what linear time operation are you talking about? And how does
having a null or invalid iterator help you?
Aug 24 '06 #2
User-Agent: OSXnews 2.081
Xref: number1.nntp.dca.giganews.com comp.lang.c++:818055
Mark P <us****@fall2005REMOVE.fastmailCAPS.fmwrote:
>Amit Bhatia wrote:
>Hi,
I am wondering if I can assign a list iterator = NULL. Suppose I have a
class A:
A.h
class A{
//ctors etc etc.
list<A>::iterator it;
}

A.C
//;;
A::some_method
{
it = NULL; /??
}

Currently, I am using a pointer instead of an iterator(Hence above
operation is perfectly valid), but when I wish to delete this particular
object from another list
list<Amylist;

I will have to do a linear time operation. However, if I can use
Or is there some analogous thing to NULL for iterators?

thanks,
amit.

There is no general NULL for iterators. The analog of a null pointer is
an iterator which is at the end of whatever it's iterating. If you have
a list l in your class, you can set "it" to l.end().

But what linear time operation are you talking about? And how does
having a null or invalid iterator help you?
Well in the list "mylist" above, if I want to delete a particular
element, and if I use mylist.remove(ANINSTANCEOF A), it takes (atmost) linear
time in length of "mylist" (as STL website says). However, if I use mylist.erase(ITERATOR THIS TIME), I think it would
take constant time.
So using a reference to a particular element by a pointer will result in
longer computation time comparitively I guess to remove it from the
list?
amit.


--
Aug 24 '06 #3
Amit Bhatia wrote:
Hi,
I am wondering if I can assign a list iterator = NULL.
No, you cannot. However, you do have a few options:

1) Use (your list).end() to represent an invalid iterator into your list
2) Wrap your iterator in something like boost::optional
3) Use an additional bool variable to indicate the validity of your iterator

--
Clark S. Cox III
cl*******@gmail.com
Aug 24 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by sam | last post: by
15 posts views Thread by sandwich_eater | last post: by
5 posts views Thread by gerg | last post: by
8 posts views Thread by freckred76 | last post: by
15 posts views Thread by jayesah | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.