title: Vector of pointers to elements of another vector instead of cloning a vector
body: <p>I would like to have a vector z, and be able to access it as <code>z[i]</code> or <code>*pz[i]</code>, where <code>*pz[i]</code> points to <code>z[i]</code>. so there is one set of values but two ways to access it. </p>
  
  <p>This is the code that I had:</p>
  
  <pre><code>std::vector&lt;double&gt; z;
  std::vector&lt;double*&gt; pz;
  
  for (int i = 0; i &lt; 5; i++) {
    z.push_back(7+0.01*i);
    std::cout &lt;&lt; i &lt;&lt; "  z = " &lt;&lt; z &lt;&lt; std::endl;
    pz.push_back(&amp;z.back());
    std::cout &lt;&lt; i &lt;&lt; "  pz = " &lt;&lt; pz &lt;&lt; std::endl;
    std::cout &lt;&lt; i &lt;&lt; "  *pz = " ;
    for (int j = 0; j &lt; pz.size(); j++) {
      std::cout &lt;&lt; "  " &lt;&lt; *pz[j];
    }
    std::cout &lt;&lt; std::endl;
  }
  
  z[1]=17.3;
  std::cout &lt;&lt; "z[1] = " &lt;&lt; z[1] &lt;&lt; std::endl;
  std::cout &lt;&lt; "*pz[1] = " &lt;&lt; *pz[1] &lt;&lt; std::endl;
  
  *pz[2]=34.1;
  std::cout &lt;&lt; "z[2] = " &lt;&lt; z[2] &lt;&lt; std::endl;
  std::cout &lt;&lt; "*pz[2] = " &lt;&lt; *pz[2] &lt;&lt; std::endl;
  </code></pre>
  
  <p>the output:</p>
  
  <pre><code>0  z = vector(1) [ 7 ]
  0  pz = vector(1) [ 0x1d00b80 ]
  0  *pz =   7
  1  z = vector(2) [ 7, 7.01 ]
  1  pz = vector(2) [ 0x1d00b80, 0x1d00bc8 ]
  1  *pz =   1.50254e-316  7.01
  2  z = vector(3) [ 7, 7.01, 7.02 ]
  2  pz = vector(3) [ 0x1d00b80, 0x1d00bc8, 0x1d00bf0 ]
  2  *pz =   1.50254e-316  7.01  7.02
  3  z = vector(4) [ 7, 7.01, 7.02, 7.03 ]
  3  pz = vector(4) [ 0x1d00b80, 0x1d00bc8, 0x1d00bf0, 0x1d00bf8 ]
  3  *pz =   1.50254e-316  7.01  7.02  7.03
  4  z = vector(5) [ 7, 7.01, 7.02, 7.03, 7.04 ]
  4  pz = vector(5) [ 0x1d00b80, 0x1d00bc8, 0x1d00bf0, 0x1d00bf8, 0x1d00c60 ]
  4  *pz =   1.50254e-316  7.01  7.02  7.03  7.04
  z[1] = 17.3
  *pz[1] = 7.01
  z[2] = 7.02
  *pz[2] = 34.1
  </code></pre>
  
  <p>There are 3 problems:</p>
  
  <ol>
  <li>The first element of <code>*pz</code> gets undefined after second push.</li>
  <li>When I assign <code>z[1]</code>, <code>*pz[1]</code> is not <code>z[1]</code> anymore.</li>
  <li>When I assign <code>*pz[2]</code>, it's not assigning to <code>z[2]</code> anymore.</li>
  </ol>
  
  <p>(In my actual program I need to have a vector to access even elements and a vector to access odd elements but I don't want to make a clone and use more memory.)</p>