I want to do a list of lists using stl but I have a problem when I try to look for an element and remove it from the list, I'm unable to find out the error.
I made a little example with randoms number:
int main(int argc, char** argv) {
list<list <int> > l;
srand ( time(NULL) );
list<list <int> >::iterator j;
list<int>::iterator i;
for(int n1=0;n1<10;n1++){
list<int> aux;
int r=rand()%10 +1;
for(int n=0;n<r;n++){
int r2=rand()%100;
aux.push_back(r2);
}
l.push_back(aux);
}
for(j=l.begin();j!=l.end();j++){
list<int> l2=(*j);
for(i=l2.begin();i!=l2.end();i++)
cout << (*i) << " -> ";
cout << endl;
}
int num;
cout << "element to delete: ";
cin >> num;
// ===== problem should be here =====
for(j=l.begin();j!=l.end();j++){
for(i=(*j).begin();i!=(*j).end();i++){
if((*i)==num){
(*j).erase(i);
}
}
}
//===================================
cout << endl;
for(j=l.begin();j!=l.end();j++){
list<int> l2=(*j);
for(i=l2.begin();i!=l2.end();i++)
cout << (*i) << " -> ";
cout << endl;
}
}
I really appreciate any help you can provide.