title: Does std::vector change its address? How to avoid
body: <p>Since vector elements are stored contiguously, I guess it may not have the same address after some push_back's , because the initial allocated space could not suffice.</p>
  
  <p>I'm working on a code where I need a reference to an element in a vector, like:</p>
  
  <pre><code>int main(){
      vector&lt;int&gt; v;
      v.push_back(1);
      int *ptr = &amp;v[0];
      for(int i=2; i&lt;100; i++)
          v.push_back(i);
      cout &lt;&lt; *ptr &lt;&lt; endl; //?
      return 0;
  }
  </code></pre>
  
  <p>But it's not necessarily true that <code>ptr</code> contains a reference to <code>v[0]</code>, right? How would be a good way to guarantee it?</p>
  
  <p>My first idea would be to use a vector of pointers and dynamic allocation. I'm wondering if there's an easier way to do that?</p>
  
  <p>PS.: Actually I'm using a vector of a class instead of int, but I think the issues are the same. </p>