M
Mike Smith
Marcus said:I am getting warnings when comparing a (regular) int to the value
returned from std::vector.size() in code similar to the following:
int i = //stuff
if (i >= vec.size())
The compiler gives me a "signed/unsigned mismatch" warning (I am using
the C++ compiler found in Visual Studio .NET 2003, which tells me that
the return type is a size_t). I get similar warnings when assigning the
return value of .size() to an int. However, even on Stroustrup's FAQ
<http://www.research.att.com/~bs/bs_faq2.html> he has:
for (int i = 0; i<v.size(); ++i)
which seems to me that it should be fine. In order to quiet the error,
I have casted the return value to an int:
if (i >= static_cast<int>(vec.size()))
Is this bad style? Is there a more elegant solution?
Why not just use size_t as the type of your loop index?