Can you pop_back a vector and still use the iterator to the last element?

醉酒当歌 提交于 2021-02-18 21:29:26

问题


I wonder what happens if I have an iterator on the last element of the vector and do pop_back.

std::set<int> s;
s.insert(5);

std::vector<int> v = {1, 2, 3, 4, 5};

for (auto it = v.begin(); it != v.end();) {
    if (s.count(*it)) {
        std::swap(*it, v.back());
        v.pop_back();
    } else {
        ++it;
    }
}

Code above works properly (v is {1, 2, 3, 4} after that block) at least with clang, but is it correct to check if it == v.end() if it is invalidated?


回答1:


Your instincts are good; vector::pop_back invalidates iterators and references to the last element. If it is such an iterator, then it will be invalidated, and thus cannot be compared with v.end().

It would be much better to fix this by using algorithms and the erase-remove idiom:

auto last_it = std::remove_if(v.begin(), v.end(), [&](const auto &val) {return s.count(val) != 0;});
v.erase(last_it, v.end());


来源:https://stackoverflow.com/questions/57629086/can-you-pop-back-a-vector-and-still-use-the-iterator-to-the-last-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!