What is the right way of iterating over a vector in C++?
Consider these two code fragments, this one works fine:
for (unsigned i=0; i < polygon.size(); i++) {
sum += polygon[i];
}
and this one
for (int i=0; i < polygon.size(); i++) {
sum += polygon[i];
}
which causes a warning:
comparison between signed and unsigned integer expressions
.
I'm new in the world of C++, so the
unsigned
variable looks a bit frightening to me and I know unsigned variables can be dangerous if not used correctly, so - is this correct?