I'm doing an exercise to reverse the order of a vector. My program works, but I am getting a warning in the for
loop I am executing below:
void reverse (vector<int>& v){
for(int i=0; i<v.size()/2; ++i){
swap(v[i],v[v.size()-1-i]);
}
}
on the line of the for
loop, I am getting the warning message "comparison between signed and unsigned integer expressions [-Wsign-compare]". I am using Stroustrop's Programming Principles and Practice Using C++, and he uses basically that general format (without the / 2). Should I be writing this for-loop differently?
v.size()
is unsigned (size_t
is unsigned). Thereforei<v.size()/2
is a signed vs. unsigned comparison. Since youri
will never be negative, you could definei
asunsigned int
oruint32_t
orsize_t
. – phonetaggeri
will never be negative, there is no real issue here. Your compiler is warning you that you might not be smart enough to understand the code that you've written, and should consider changing it to something that the compiler writer approves of. – Pete Becker