So, what is the difference between these two statement:
for(auto i : VectorName){} for(auto i = VectorName.begin(); i != VectorName.end(); i++){} For example, I have this program:
#include <iostream> #include <string> #include <vector> using namespace std; int main() { vector<char> vec = {'H','e','l','l','o','W','o','r','l','d','!'}; for(auto i = vec.begin(); i != vec.end(); i++) // This loop has error { cout << i << endl; } for(auto k : vec) //This loop has no problem { cout << k << endl; } return 0; } I am confused because in this example in this Microsoft docs:
// cl /EHsc /nologo /W4 #include <deque> using namespace std; int main() { deque<double> dqDoubleData(10, 0.1); for (auto iter = dqDoubleData.begin(); iter != dqDoubleData.end(); ++iter) { /* ... */ } // prefer range-for loops with the following information in mind // (this applies to any range-for with auto, not just deque) for (auto elem : dqDoubleData) // COPIES elements, not much better than the previous examples { /* ... */ } for (auto& elem : dqDoubleData) // observes and/or modifies elements IN-PLACE { /* ... */ } for (const auto& elem : dqDoubleData) // observes elements IN-PLACE { /* ... */ } } They noted that the range for statement is not better than the regular one.