473,231 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

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 23460
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: sam | last post by:
Hi, This is a "list iterator" problem I expect it will copy the list iterator (l_iter) to the caller: eg. list<HashMap>::iterator AcctConfParser::find_Acct_rule(string i) {...
15
by: sandwich_eater | last post by:
I want to know how to set an std::list iterator variable to make it null or nil. If this is not possible what is the value of an uninitialised std::list iterator and is it ok to assign this value...
5
by: gerg | last post by:
I'm having to deal with some legacy code which I think may be causeing some problems. Can STL pro's please add comments about the correctness of the following code: class A { public: /// ......
8
by: freckred76 | last post by:
Hi, I think this might be a VC++ problem. I am using Microsoft Visual Studio 2005 Full Version (8.0). I have a simple for loop that iterates over a list using the standard iterator. The...
6
by: Jason S | last post by:
where is the behavior of list::iterator specified? I need to keep track of ranges of data, all I can figure out so far is that iterators are guaranteed to be valid unless you remove the elements...
15
by: jayesah | last post by:
Hi All, List and its iterator work as following way : list<intmylist; list<int>::iterator itr; itr = mylist.begin(); cout << (*itr); But I want something like this:
2
by: Rohit.MEHTA | last post by:
Hi All I want to overload the ++ and - operator for stl list iterator so that I could do some thing additional in that As want to set the current node status also Can some one please post...
1
by: David Bilsby | last post by:
All Apologies for cross posing this but I am not sure if this is a VC 8 STL bug or simply an invalid use of the iterator. I have a PCI card access class which basically abstracts a third party...
2
by: jaki1910 | last post by:
Hi guys correct me plz: i have this error just when i put liste.erase(it); Debug assertion Failed .... Expression: List iterator not incrementable. for(list<student>::iterator...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.