Question

If I have an integer vector

vector<int> vec;

and I loop in the following way

for(int i=0; i<vec.size(); i++)
{
   // do something
}

I get the signed/unsigned mismatch warning.

Of course I can declare i of type size_t to solve the problem. But if I keep i as int could I get some problem at runtime?

Was it helpful?

Solution 2

Sure, if vec.size() is larger than than the maximum value for a signed int.

You can find maximum values in limits.h per this table.

OTHER TIPS

Use the vector iterator instead.

vector<int>::iterator it;
for (it = vec.begin(); it!= vec.end(); ++it)
{
   //do something
}

Yes. For big numbers more than a value which a signed can store, it will be an undefined behavior.

However for small numbers which can be store in a signed it's OK.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top