Keeping std::list iterators valid through insertion

前端 未结 4 1551
南笙
南笙 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 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.

提交回复
热议问题