Warning: comparison between signed and unsigned integer expressions

From Cppsyntax

Jump to: navigation, search

You can get the error

  Warning: comparison between signed and unsigned integer expressions

if you try to use the cast of the size of a vector as an int.

WRONG

 vector<int> s;
 for (int i = 0; i<s.size(); ++i) 
   s[i]++;

RIGHT

 vector<int> s;
 for (int i = 0; i<(int)s.size(); ++i)    // cast
   s[i]++;

ALSO RIGHT

 vector<int> s;
 for (unsigned int i = 0; i<s.size(); ++i) // unsigned int
   s[i]++;
Personal tools