Possible Duplicate:
C++ STL: Which method of iteration over a STL container is better?
Efficiency of vector index access vs iterator access
Assuming an std::vector<int> named numbers, which of the following is faster?
for (vector<int>::iterator i = numbers.begin(); i != numbers.end(); i++)
cout << *i;
or..
for (int i = 0; i < numbers.size(); i++)
cout << numbers.at(i);
Which one is faster? Is there any significant difference?
Those two aren’t even comparable. The former loops through each element, while the latter loops through while checking if the current index is valid (with
at()). Take out the check and ask again:Now they do the same thing. And now the question is a duplicate (#1 #2). To summarize: it doesn’t matter.