You should use std::vector<T>::size_type 1 . Its unsigned integral type. Its usually the same as size_t .
To find out the difference between size_type and size_t , see this section:
1. Similarly, you can use std::string::size_type , std::list<T>::size_type , std::deque<T>::size_type , std::set<T>::size_type and so on . Almost all standard containers define a nested type called size_type .
It can be argued that an iterator must be used instead of an index. But I also see that once an iterator makes the for-loop very wide horizontally, see This:
for(std::vector<std::vector<std::string> >::iterator it = v.begin(); it != v.end(); ++it) { }
It does not look. This is sometimes annoying. In such situations, some programmers prefer indexes over an iterator.
In C ++ 0x, the iterator has become more idiomatic. Now you can use the iterator without making the syntax cumbersome:
for(auto it = v.begin(); it != v.end(); ++it) { }
Or even better, using a range for the loop:
for(auto & item : v) { }
Nawaz
source share