title: Resizing a C++ std::vector<char> without initializing data
body: <p>With vectors, one can assume that elements are stored contiguously in memory, allowing the range <em>[&amp;vec[0], &amp;vec[vec.capacity())</em> to be used as a normal array. E.g.,</p>
  
  <pre><code>vector&lt;char&gt; buf;
  buf.reserve(N);
  int M = read(fd, &amp;buf[0], N);
  </code></pre>
  
  <p>But now the vector doesn't know that it contains <em>M</em> bytes of data, added externally by <em>read()</em>. I know that <em>vector::resize()</em> sets the size, but it also clears the data, so it can't be used to update the size after the <em>read()</em> call.</p>
  
  <p>Is there a trivial way to read data <em>directly</em> into vectors and update the size after? Yes, I know of the obvious workarounds like using a small array as a temporary read buffer, and using <em>vector::insert()</em> to append that to the end of the vector:</p>
  
  <pre><code>char tmp[N];
  int M = read(fd, tmp, N);
  buf.insert(buf.end(), tmp, tmp + M)
  </code></pre>
  
  <p>This works (and it's what I'm doing today), but it just bothers me that there is an extra copy operation there that would not be required if I could put the data <em>directly</em> into the vector.</p>
  
  <p>So, is there a simple way to modify the vector size when data has been added externally?</p>