Generally, please use iterator or range for to traverse the vector. If you must use index, you need to pay attention to whether the length of the vector will change during the loop.
std::vector<int> vi;
for (auto it = vi.begin(); it != vi.end(); ++it)
// do something
for (auto & i : vi)
// do something
If parameters such as optimization o2 are specified, it may be optimized. If you do not modify the size of the vector in the loop, in fact, because the size of the vector is stored in a variable, this process is not very expensive.
Generally, please use iterator or range for to traverse the vector. If you must use index, you need to pay attention to whether the length of the vector will change during the loop.
If parameters such as optimization o2 are specified, it may be optimized. If you do not modify the size of the vector in the loop, in fact, because the size of the vector is stored in a variable, this process is not very expensive.