The loop will work with 'i' as an int or unsigned int perfectly fine in this scenario, but might generate compiler warnings.
The main idea is, you want to make the type of 'i' match the type of whatever you're comparing it with. So 'i' should have the same type as 'vector.size()'.
In this case, the proper way to loop is use vector<T>::size_type:
for(vector<int>::size_type i = 0; i < vector.size(); ++i)
Thanks guys for all your explanations... Now I think I get something with this.
Two more questions: if we define an iterator in a "for" loop, when the loop is finished, the iterator is destroyed automatically. What about if we define an iterator in a function? Shall we destroy is after usage in the function? or we can just leave it in the function and when the function is finished the iterator will be destroyed automatically?