std::list::erase not working

后端 未结 3 1110
天涯浪人
天涯浪人 2020-12-20 08:13

I am trying to delete an element from a list of objects if one of the object\'s properties matches a condition. This is my function to do so, however, after performing this

3条回答
  •  不思量自难忘°
    2020-12-20 08:37

    Calling erase() when an iterator is iterating over the list invalidates the iterator. Add the elements to erase to a second list then remove them afterwards.

    Also note that you are passing the list by value rather than using a reference or a pointer. Did you mean to use list& listToDeleteFrom or list* listToDeleteFrom?

提交回复
热议问题