About C++ STL list class method erase() -
the documentation of list::erase()
says, "call destructor before", mean? if want erase(it)
item , push_back(*it)
item again, illegal since destructed already?
yes, result in undefined behavior. once erase
list iterator invalidate iterator, meaning object references no longer guaranteed valid. means if try use iterator in context, including trying dereference value add list again, result in undefined behavior, crash program, overwrite important memory, or nothing.
if want move element of list back, consider using list's splice
method:
mylist.splice(mylist.end(), mylist, it);
this moves element end without making copy.
Comments
Post a Comment