Keeping std::list iterators valid through insertion

前端 未结 4 1453
南笙
南笙 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:48

    I was indeed being dense. The standard gives us all the tools we need. Specifically, the sequence container requirements 23.2.3/9 say:

    The iterator returned from a.insert(p, i, j) points to the copy of the first element inserted into a, or p if i == j.

    Next, the description of list::insert says (23.3.5.4/1):

    Does not affect the validity of iterators and references.

    So in fact if pos is my current iterator inside the list which is being consumed, I can say:

    auto it = buf.insert(buf.end(), newdata.begin(), newdata.end());
    
    if (pos == buf.end()) { pos = it; }
    

    The range of new elements in my list is [it, buf.end()), and the range of yet unprocessed elements is [pos, buf.end()). This works because if pos was equal to buf.end() before the insertion, then it still is after the insertion, since insertion does not invalidate any iterators, not even the end.

    0 讨论(0)
  • 2020-12-20 21:49

    list<char> 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<char> 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.

    0 讨论(0)
  • 2020-12-20 21:51

    From http://www.sgi.com/tech/stl/List.html

    "Lists have the important property that insertion and splicing do not invalidate iterators to list elements, and that even removal invalidates only the iterators that point to the elements that are removed."

    Therefore, readpos should still be valid after the insert.

    However...

    std::list< char > is a very inefficient way to solve this problem. Each byte you store in a std::list requires a pointer to keep track of the byte, plus the size of the list node structure, two more pointers usually. That is at least 12 or 24 bytes (32 or 64-bit) of memory used to keep track of a single byte of data.

    std::deque< char> is probably a better container for this. Like std::vector it provides constant time insertions at the back however it also provides constant time removal at the front. Finally, like std::vector std::deque is a random-access container so you can use offsets/indexes instead of iterators. These three features make it an efficient choice.

    0 讨论(0)
  • 2020-12-20 22:11
    if (readpos == buf.begin())
    {
        buf.insert(buf.end(), newdata.begin(), newdata.end());
        readpos = buf.begin();
    }
    else
    {
        --readpos;
        buf.insert(buf.end(), newdata.begin(), newdata.end());
        ++readpos;
    }
    

    Not elegant, but it should work.

    0 讨论(0)
提交回复
热议问题