You should use std::vector<T>::size_type
1. Its unsigned integral type. Its usually same as size_t
.
To know the difference between size_type
and size_t
, see this topic:
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
.
One can argue that you should use iterator instead of index. But I also see that sometime 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 doesn't look. It in fact irritates sometimes. In such situations, some programmers prefers index over iterator.
In C++0x, iterator has been made more idiomatic. Now you can use iterator without making the syntax cumbersome:
for(auto it = v.begin(); it != v.end(); ++it)
{
}
Or even better, using range-based for loop:
for(auto & item : v)
{
}