Keeping std::list iterators valid through insertion

前端 未结 4 1540
南笙
南笙 2020-12-20 21:16

Note: This is not a question whether I should \"use list or deque\". It\'s a question about the validity of iterators in the face of insert().


4条回答
  •  攒了一身酷
    2020-12-20 21:49

    list is a very inefficient way to store a string. It is probably 10-20 times larger than the string itself, plus you are chasing a pointer for every character...

    Have you considered using std::dequeue instead?

    [edit]

    To answer your actual question, adding and removing elements does not invalidate iterators in a list... But end() is still going to be end(). So you would need to check for that as a special case at the point where you insert the new element in order to update your readpos iterator.

提交回复
热议问题