Vector.size() vs iterating
Given a std::vector<MyClass> named myVec:
Code:
// Loop version 1
for (int i = 0; i < myVec.size(); ++i) {
// Do something with myVec[i]
}
// Loop version 2
for (std::vector<MyClass>::iterator it = myVec.begin();
it != myVec.end(); ++it) {
// Do something with *it
}
// Loop version 3
for (auto &x : myVec) {
// Do something with x
}
Tracing my code in a debugger I am getting size = 0, loop version 1 does not execute, and loop versions #2 and #3 enter the loop and cause an access violation.
My suspicion is that I'm writing out of bounds somewhere and corrupting the vector's internal state, but wanted to check if you guys know of any legitimate case when this could happen.
Thoughts?