title: C++ vector performance: iterator access versus brackets operator access
body: <p>I'm wondering whether iterator will outperform brackets operator in a sequential access setting. Consider the following code snippets:</p>
  
  <pre><code>// v1.1
  for (std::vector&lt;int&gt;::const_iterator it = v.begin(); it != v.end(); ++it) {
      do_something(*it);
  }
  
  // v1.2
  for (size_t i = 0; i != v.size(); ++i) {
      do_something(v[i]);
  }
  
  // v1.3
  std::vector&lt;int&gt;::const_iterator endIt = v.end();
  for (std::vector&lt;int&gt;::const_iterator it = v.begin(); it != endIt; ++it) 
      do_something(*it);
  }
  
  // v1.4
  size_t size = v.size();
  for (size_t i = 0; i != size; ++i) {
      do_something(v[i]);
  }
  
  // v2.1
  for (std::vector&lt;int&gt;::iterator it = v.begin(); it != v.end(); ++it) {
      *it = it - v.begin(); // iterator arithmetic; expensive or not?
  }
  
  // v2.2
  for (size_t i = 0; i != v.size(); ++i) {
      v[i] = i;
  }
  </code></pre>
  
  <p>Has anyone ever done a benchmark for these? (Well, it depends on the compiler; let's take g++ 4.7 or 4.8 for instance.) Thanks.</p>