Consider the following code example :
#include<iostream>
#include<vector>
#include<typeinfo>
int main(){
std::vector<int> v = {1 ,2,4};
for(auto &i:v) std::cout<<typeid(i).name();
for(auto k = v.begin(); k!=v.end();++k) std::cout<<typeid(k).name();
return 0;
}
The first loop represents range based for-loops , and second one are regular for loops with iterators. I have used regular ones a lot , and from my experience , auto k is of type of iterator , while range based loops had type of auto i as int. Output of above program is:
i & N9__gnu_cxx17__normal_iteratorIPiSt6vectorIiSaIiEEEE
Is this normal behavior for range based for loops over collections like vectors ( somewhere mentioned) ? Because someone (like me) would assume range based for loops are just shorthand for regular for loops.
forloop gives you the elements. Is that a good enough answer?end()in every turn and therefore can adapt to structural changes of the vector (as long as they don't invalidatek). The range-based loop evaluatesend()only at the beginning and therefore you better leave the vector's structure alone inside. Therefore it makes sense that you're not provided with the means to change that structure (i.e. iterators), but only with the means to access/change the values.