title: For C++ Random Access Iterator (vector iterator), how is the difference between iterators calculated?
body: <p>I have following code to randomize the elements in a Random Access Iterator (<code>vector&lt;int&gt;::iterator</code>) -</p>
  
  <pre><code>#include &lt;vector&gt;
  #include &lt;list&gt;
  #include &lt;iterator&gt;
  #include &lt;algorithm&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;iostream&gt;
  using namespace std;
  
  template&lt;class RandomAccesIterator&gt;
  void randomize(RandomAccesIterator iterBegin, RandomAccesIterator iterEnd)
  {
      while (iterBegin != iterEnd)
      {
          int rand1 = rand();
          auto iterdiff = iterEnd - iterBegin;
          auto secondarg = iterBegin + rand1 % (iterdiff);
          iter_swap(iterBegin, secondarg);
          ++iterBegin;
      }
  }
  </code></pre>
  
  <p>And following is the main() function:</p>
  
  <pre><code>int main()
  {
      //container used as to apply algorithm to.
      list&lt;int&gt; List = {34,77,16,2,35,76,18,2,56};
  
      //randomize example.
      cout &lt;&lt; "calling randomize on sorted vector: " &lt;&lt; endl;
      List.sort();
      vector&lt;int&gt; temp(List.begin(), List.end());
      cout &lt;&lt; "before randomize: " &lt;&lt; endl;
      for (vector&lt;int&gt;::iterator it = temp.begin(); it != temp.end(); it++)
      {
          cout &lt;&lt; *it &lt;&lt; " ";
      }
      cout &lt;&lt; endl;
  
      randomize(temp.begin(),temp.end());
      cout &lt;&lt; "after randomize: " &lt;&lt; endl;
      for (vector&lt;int&gt;::iterator it = temp.begin(); it != temp.end(); it++)
      {
          cout &lt;&lt; *it &lt;&lt; " ";
      }
      cout &lt;&lt; endl&lt;&lt;endl;
      return 0;
  }
  </code></pre>
  
  <p>In the randomize template function, how is the difference between iterators calculated (iterEnd - iterBegin)?</p>
  
  <p>I tried a couple of things in the immediate window, and it looks like iterEnd - iterBegin is calculated like so (the vector has 9 elements in it, and the below calculation gives 9). I tried with various number of elements in the <code>vector&lt;int&gt;</code>, and the answer was correct each time. The computation is for the first time we encounter iterEnd - iterBegin in the while loop (that is, for 9 elements in the vector):</p>
  
  <p>In the immediate window -</p>
  
  <p>1.</p>
  
  <pre><code>iterEnd
  {-33686019}
      [ptr]: 0x0080f9dc {-33686019}
      [Raw View]: {...}
  </code></pre>
  
  <p>2.</p>
  
  <pre><code>iterBegin
  {2}
      [ptr]: 0x0080f9b8 {2}
      [Raw View]: {...}
  </code></pre>
  
  <p>3.</p>
  
  <pre><code>0x0080f9dc-0x0080f9b8 //iterEnd - iterBegin gives 36.
  36
  </code></pre>
  
  <p>4.</p>
  
  <pre><code>36/4 //Dividing 36 by 4, since an integer is 4 bytes (we are iterating over a vector of integers).
  9
  </code></pre>
  
  <p>I also tried with 8 elements in the <code>vector&lt;int&gt;</code>, and the same type of calculation resulted in 8 elements in step 4. above.</p>
  
  <p>I have a couple of questions here:</p>
  
  <ol>
  <li>Are the steps that I am performing to get the number of elements in the vector correct (steps 1. to 4. above)?</li>
  <li>In step 4. above, I am dividing 36, which is in decimal - by 4 bytes. How is this giving me the correct result? It would make sense if I were dividing 36 bytes by 4 bytes, and then that would give me 9 elements. Why is dividing <strong>decimal 36</strong> by 4 bytes giving me the correct answer?</li>
  </ol>
  
  <p>Please see: I am using the following compiler: Microsoft Visual Studio Enterprise 2019 (Version 16.2.1). The operating system platform is 64 bit, x64-based processor. I am building on Debug x86 environment. The Windows edition is Windows 10 Pro </p>