473,756 Members | 5,955 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

List Iterator = NULL?

User-Agent: OSXnews 2.081
Xref: number1.nntp.dc a.giganews.com comp.lang.c++:8 18044
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>::iterat or 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 23515
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>::iterat or 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.dc a.giganews.com comp.lang.c++:8 18055
Mark P <us****@fall200 5REMOVE.fastmai lCAPS.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>::itera tor it;
}

A.C
//;;
A::some_meth od
{
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(A NINSTANCEOF A), it takes (atmost) linear
time in length of "mylist" (as STL website says). However, if I use mylist.erase(IT ERATOR 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
4350
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) { list<HashMap>::iterator l_iter; HashMap::iterator m_iter; for (l_iter=macro_list.begin(); l_iter!=macro_list.end(); l_iter++) {
15
19754
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 to a std::list iterator variable (or is it better to use a seperate bool variable as a "null flag" ?) e.g. struct mystru { std::string nm;
5
22835
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: /// ... assume constructors and such are defined char* sz; // pointer to memory buffer };
8
16952
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 list is populated with a struct _BLOCK defined as: typedef struct {
6
3718
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 they point to. Example -- if L is a list and Li = L.end(), and you call L.push_back(something), how do you know whether Li will still point to the list's end, or whether it will point to the something? I tried this program in Microsoft VC++ 6.0...
15
2801
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
3675
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 sample code to use this, I would NOT like to do it by inheriting the iterator class. Rather I would like to keep a member iterator and wrap the logic around it
1
6241
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 library from the higher level classes. This low level class is used to allocate locked pages of memory before doing a DMA. The third party library locks memory and returns a pointer to an internal structure, which we can simply cast to a (void...
2
8551
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 it=liste.begin();it!=liste.end();it++){ if((it->getsexe()=='f')||(it->getsexe()=='F')){
0
10040
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9873
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9846
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9713
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5142
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3806
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2666
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.