title: C++: Division by vector.size() gives strange results
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/5328269/int-divided-by-unsigned-int-causing-rollover">int divided by unsigned int causing rollover</a>  </p>
  </blockquote>
  
  
  
  <p>Hi I am doing the following:</p>
  
  <pre><code>struct coord{
      int col;
  
  };
  
  
  int main(int argc, char* argv[]) {
  
      coord c;
      c.col = 0; 
  
      std::vector&lt;coord&gt; v;
  
      for(int i = 0; i &lt; 5; i++){
          v.push_back(coord());
      }
  
      c.col += -13;
  
  
      cout &lt;&lt; " c.col is " &lt;&lt; c.col &lt;&lt; endl;
      cout &lt;&lt; " v size is " &lt;&lt; v.size() &lt;&lt; endl;
  
      c.col /= v.size();
  
  
      cout &lt;&lt; c.col &lt;&lt; endl;
  
  }
  </code></pre>
  
  <p>and I get the following output:</p>
  
  <pre><code> c.col is -13
   v size is 5
  858993456
  </code></pre>
  
  <p>However, if I change the division line to <code>c.col /= ((int)v.size());</code> I get the expected output: </p>
  
  <pre><code> c.col is -13
   v size is 5
  -2
  </code></pre>
  
  <p>Why is this?</p>