473,495 Members | 2,058 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

list iterator

There's something I don't quite understand, sorry if that sound stupid
or whatever ; )

why is :

for( it = Flare::flareList.begin() ; it != Flare::flareList.end(); it++)
if (it->isWorn())
{
it = Flare::flareList.erase( it);
// Flare::flareList.push_back( Flare());
}

not working ? Since erase is supposed to return a valid iterator or the
end of the list... I made a workaround but it doens't look so cool to me
bool through;
do
{
through = true;

for( it = Flare::flareList.begin() ; it != Flare::flareList.end(); it++)
if (it->isWorn())
{
it = Flare::flareList.erase( it);
// Flare::flareList.push_back( Flare());
through = false;
break;
}
}
while (!through);
That works but it's neither efficient nor pretty, so if anyone could
explain me what's going on in the first case and a better work around ;)
thanks !
Jul 23 '05 #1
2 1438
Flwz a écrit :
There's something I don't quite understand, sorry if that sound stupid
or whatever ; )

why is :

for( it = Flare::flareList.begin() ; it !=
Flare::flareList.end(); it++)
if (it->isWorn())
{
it = Flare::flareList.erase( it);
// Flare::flareList.push_back( Flare());
}

not working ? Since erase is supposed to return a valid iterator or the
end of the list... I made a workaround but it doens't look so cool to me


Ok alright, I found out (why is it always just after I post ? ;)
if it is at the end of the list after the erase, it still does it++
before checking the end for statement, so it obviously will crash ; )
Jul 23 '05 #2
Flwz wrote:
There's something I don't quite understand, sorry if that sound stupid
or whatever ; )

why is :

for( it = Flare::flareList.begin() ; it !=
Flare::flareList.end(); it++)
if (it->isWorn())
{
it = Flare::flareList.erase( it);
You assign the value here and also do it++ after every iteration. You
should only do either of them.
// Flare::flareList.push_back( Flare());
}

not working ? Since erase is supposed to return a valid iterator or the
end of the list... I made a workaround but it doens't look so cool to me
bool through;
do
{
through = true;

for( it = Flare::flareList.begin() ; it !=
Flare::flareList.end(); it++)
if (it->isWorn())
{
it = Flare::flareList.erase( it);
// Flare::flareList.push_back( Flare());
through = false;
break;
}
}
while (!through);
That works but it's neither efficient nor pretty, so if anyone could
explain me what's going on in the first case and a better work around ;)
thanks !


for (it = Flare::flareList.begin(); it != Flare::fllareList.end(); )
{
if (it->isWorn())
it = Flare::flareList.erase(it);
else
++it;
}

should do it.

V
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
3483
by: kazio | last post by:
Hello, So, I need to have double linked, circular list (last element point to the first one and first one points to the last one). I thought maybe I could use list container from STL, but...
12
10894
by: Brett L. Moore | last post by:
Hi, I have had trouble determining whether the STL list.size() operation is O(1) or O(n). I know the list is a doubly-linked list, so if the size() operation begins at the head, then counts to...
4
1450
by: Vince | last post by:
Hi, I am not used to template and I cannot find what's wrong with this code : Compiler(MSVS 7) complains at line : class iterator : public std::iterator<... Here is the source code
6
6634
by: PengYu.UT | last post by:
Hi, Suppose I have a list which contains pointers. I want the pointer got by dereferencing the iterator be a pointer pointing to a const object. But std::list<const T*>::const_iterator doens't...
3
2114
by: janzon | last post by:
Hi! Sorry for the bad subject line... Here's what I mean. Suppose we deal with C++ standard integers lists (the type is indifferent). We have a function f, declared as list<intf(int); Now...
15
2765
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:
6
1796
by: Henrik Goldman | last post by:
Hello, I have a dataset which consist of a string username and string hostname as a key and then an integer representing a count as the matching "second" value in a pair. So far I've used...
12
9426
by: Howard | last post by:
Is there an easy way to get an iterator (*not* a reverse-iterator) to the last element in a list? The last() function returns the element itself, not an iterator. Thanks, -Howard
1
6219
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...
6
3249
by: APEJMAN | last post by:
I know what I'm posting here is wired, but it's been 3 days I'm workin g on these codes, but I have no result I post the code here I dont wanne bother you, but if any one of you have time to...
0
7120
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
6878
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...
0
5456
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4897
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4583
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...
0
3088
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...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1405
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 ...
1
649
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.