As mentioned std::vector::push_back()
may invalidate your iterator. Possible, but pretty ugly solution could be:
for (auto beg = v.begin(); beg != v.end();++beg)
{
if (beg == v.begin()) {
v.push_back(50);
beg = v.begin();
}
}
but your logic seems convoluted, why not push back just before the loop?
4
solved C++ why this code doesn’t work? [closed]