473,385 Members | 1,848 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,385 software developers and data experts.

Deleting elements from a STL List conditionally

Hi,
WeĦre getting a segmentation fault because weĦre trying to delete elements from a List while traversing it, i.e.:

Expand|Select|Wrap|Line Numbers
  1. void Character::deleteProcessed(list<Line> &active,int y){
  2.     bool erasedAtTheEnd = false;
  3.     for (list<Line>::iterator it = active.begin();it != active.end()&&!erasedAtTheEnd;){
  4.         if (it->getHighPoint().getY() < (HeightFloat/Height)*(y+1)){
  5.             Line aux = *it;
  6.             it++;
  7.             //In case you try to delete the final element ,  it != end  doesn't 
  8.                         //stop the loop
  9.             if (it==active.end()) 
  10.                 erasedAtTheEnd = true;
  11.             active.remove(aux);
  12.         }
  13.         else it++;
  14.     }
  15. }
  16.  
Nov 21 '06 #1
1 1880
Colloid Snake
144 100+
I know for sure in Java(not 100% sure about C++), that when you declare an iterator and use it to traverse a list, you cannot manipulate the list, by design. Have you tried a for loop with a counter that is cast as a 'Line' object to traverse the list? That might let you bypass (if it is the same in C++ as Java) the whole iterator/manipulation thing.
Nov 21 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: SnuSnu | last post by:
Okay - here's a (probably) really easy question: I can't do the following, so what's the best way to handle this case of wanting to delete within a loop? x = deletion_list = for i in...
5
by: flupke | last post by:
Hi, i'm having trouble with deleting elements from a list in a for loop ============== test program ============== el = print "**** Start ****" print "List = %s " % el index = 0 for line...
2
by: KraftDiner | last post by:
I have a list, and within it, objects are marked for deletion. However when I iterate through the list to remove objects not all the marked objects are deleted.. here is a code portion: i = 0...
1
by: Varun Kacholia | last post by:
I apologize if there exists a standard way of deleting multiple elements from a STL hash_multiset (or even multiset for that matter) that I am unaware of. The problem, I see, with multisets is...
25
by: Markus Svilans | last post by:
Hi, There seems to be some functionality missing from the STL. I am iterating through a linked list (std::list) using a reverse iterator and attempting to erase certain items from the list. It...
3
by: | last post by:
I'm using the DataList and GridView controls, and I am trying to wrap my head around the problem of conditionally showing or hiding cells/cell content based on the presence or absence of DB data. I...
9
by: Rhamphoryncus | last post by:
The problems of this are well known, and a suggestion for making this easier was recently posted on python-dev. However, I believe this can be done just as well without a change to the language. ...
7
nabh4u
by: nabh4u | last post by:
hi, i have a double linked list containing some elements and i have a vector which stores the address of the elements in the list (pointer to the list). i want to delete a value from the list,like...
11
by: Robert Latest | last post by:
Hello, From a list of strings I want to delete all empty ones. This works: while '' in keywords: keywords.remove('') However, to a long-term C programmer this looks like an awkward way of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...

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.