문제

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?

도움이 되었습니까?

해결책 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.

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top