C
Christoff Pale
Hi,
suppose I have a list of strings;
I have to iterate of the strings and delete certain entries.
I and not sure how to do this?
for example:
#include<list>
#include<string>
#include<iostream>
int main(){
list<string> l;
l.push_back("first");
l.push_back("second");
l.push_back("deleteme");
l.push_back("fourth");
l.push_back("deleteme");
l.push_back("sixth");
// I want to erase entries that say "deleteme"
I can get and iterator, but that screws up things
list<string>::iterator it=l.begin();
for( ; it!=l.end(); it++){
if( (*it).compare("deleteme")==0){
l.erase(it);
}
}
}
This gives me segmentation fault
I think that when I delete the first "deleteme" the
it now points to "fourth";
Basically, this is what I want.
I will have a huge list of some object.
I want to traverse it all the way and delete
objects that meet a certain criteria.
any help will be much appreciated.
thanks
suppose I have a list of strings;
I have to iterate of the strings and delete certain entries.
I and not sure how to do this?
for example:
#include<list>
#include<string>
#include<iostream>
int main(){
list<string> l;
l.push_back("first");
l.push_back("second");
l.push_back("deleteme");
l.push_back("fourth");
l.push_back("deleteme");
l.push_back("sixth");
// I want to erase entries that say "deleteme"
I can get and iterator, but that screws up things
list<string>::iterator it=l.begin();
for( ; it!=l.end(); it++){
if( (*it).compare("deleteme")==0){
l.erase(it);
}
}
}
This gives me segmentation fault
I think that when I delete the first "deleteme" the
it now points to "fourth";
Basically, this is what I want.
I will have a huge list of some object.
I want to traverse it all the way and delete
objects that meet a certain criteria.
any help will be much appreciated.
thanks