List Iterator = NULL?

A

Amit Bhatia

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<A> mylist;

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.
 
M

Mark P

Amit said:
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<A> mylist;

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?
 
A

Amit Bhatia

Mark P said:
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.




--
 
C

Clark S. Cox III

Amit said:
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::eek:ptional
3) Use an additional bool variable to indicate the validity of your iterator
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top