dimanche 2 septembre 2018

Nested for loops and repeated iterator

Consider the following nested for loops. I am having trouble figuring out why when the inner for loop exits, the outer for loop attempts to increment the iterator when clearly it should not be doing so. This results in a seg fault. Why would the outer for loop continue running if it has the condition cbeg != vec.cend(). Clearly that condition was checked in the inner for loop with no problem, and it exited correctly.

I don't believe any iterators are being invalidated here, the only operation is an increment.

vector<int> vec{1,2,3,4};

for (auto cbeg = vec.cbegin(); cbeg != vec.cend(); cbeg++)
{
   for (; cbeg != vec.cend(); cbeg++) {
   }
}

What am i missing here?

Aucun commentaire:

Enregistrer un commentaire