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?
Use the vector iterator
instead.
vector<int>::iterator it;
for (it = vec.begin(); it!= vec.end(); ++it)
{
//do something
}
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 .
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.