title: What's this unexpected std::vector behavior?
body: <p>I found something surprising with <code>std::vector</code> that I thought I'd ask about here to hopefully get some interesting answers. </p>
  
  <p>The code below simply copies a string into a char vector and prints the contents of the vector in two ways. </p>
  
  <pre><code>#include &lt;vector&gt;
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  
  int main()
  {
      std::string s("some string");
      std::vector&lt;char&gt; v;
      v.reserve(s.size()+1);
  
      // copy using index operator
      for (std::size_t i=0; i&lt;=s.size(); ++i)
          v[i] = s[i];
  
      std::cout &lt;&lt; "&amp;v[0]:     " &lt;&lt; &amp;v[0] &lt;&lt; "\n";
      std::cout &lt;&lt; "begin/end: " &lt;&lt; std::string(v.begin(), v.end()) &lt;&lt; "\n";
  
      // copy using push_back
      for (std::size_t i=0; i&lt;=s.size(); ++i)
          v.push_back(s[i]);
  
      std::cout &lt;&lt; "&amp;v[0]:     " &lt;&lt; &amp;v[0] &lt;&lt; "\n";
      std::cout &lt;&lt; "begin/end: " &lt;&lt; std::string(v.begin(), v.end()) &lt;&lt; "\n";
  
      return 0;
  }
  </code></pre>
  
  <p>Building and running this yields:</p>
  
  <pre><code>$ g++ main.cpp -o v &amp;&amp; ./v
  &amp;v[0]:     some string
  begin/end: 
  &amp;v[0]:     some string
  begin/end: some string
  </code></pre>
  
  <p><strong>My expectation was that it would print the string correctly in both cases</strong>, but assigning character by character using the <strong>index operator doesn't print anything when later using <code>begin()</code> and <code>end()</code> iterators</strong>.</p>
  
  <p>Why isn't <code>end()</code> updated when when using <code>[]</code>? If this is intentional, what's the reason it's working like this?</p>
  
  <p>Is there a reasonable explanation for this behaviour? :)</p>
  
  <p>I've only tried this with gcc 4.6.1 so far.</p>