+ 1

How to avoid iterator invalid post erase in a function

Refer below code : It just demonstrate add to list and erase from list... Write now code works as it is of int and I have updated iterator in statement related to the erase Assume that below four lines are in a seperate function: if (*itrA == 5 && *itrB == 3) myList.push_back(15); if (*itrA == 5 && *itrB == 10) itrB = myList.erase(itrB); And also assume that iterator is pointing to class pointer instead of int... Itra and itrb is assigned to a local pointer variable and passed those variables to function which push_backs and erases... How to avoid iterator invalid now in function ? Feel free to ask for queries. If it is not clear, let me know and will create a full scenario in sample code https://code.sololearn.com/cCVaR6OkyIeg/?ref=app

16th Feb 2022, 4:15 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
1 Answer
+ 2
There is a function l.remove_if(lambda) Or you can use a new list to add elements in the list because its just pointers
19th Feb 2022, 11:43 AM
ĐœĐ°Ń€Ń‚ĐžĐœ đŸ˜‘đŸŽ”
ĐœĐ°Ń€Ń‚ĐžĐœ đŸ˜‘đŸŽ” - avatar