You will get this warning because the size of the container in C ++ is an unsigned type, and mixing signature / unsigned types is dangerous.
What do i usually do
for (int i=0,n=v.size(); i<n; i++) ....
this is, in my opinion, the best way to use indexes, because using an unsigned type for an index (or container size) is a logical mistake.
Unsigned types should only be used when you care about bit representation and when you intend to use modulo- (2 ** n) overflow behavior. Using unsigned types just because the value is never negative is nonsense.
A typical mistake of using unsigned types for sizes or indexes is, for example,
// Draw all lines between adjacent points for (size_t i=0; i<pts.size()-1; i++) drawLine(pts[i], pts[i+1]);
the code above is UB when the array of points is empty, because in C ++ 0u-1 there is a huge positive number.
The reason C ++ uses an unsigned type for container size is because this choice is a historical legacy from 16-bit computers (and IMO, given the semantics of C ++ with unsigned types, it was the wrong choice even then).
6502
source share