title: How to treat a std::vector like a C buffer efficiently?
body: <p>With C buffer, I often do like this:</p>
  
  <pre><code>BYTE buffer[MAX_SIZE];
  int dataSize = 0;
  
  while (appRunning()) 
  {
      dataSize += Receive(buffer + dataSize, MAX_SIZE - dataSize);
  
      int processedSize = ProcessBuffer(buffer, dataSize);
      ASSERT(processedSize &lt;= dataSize);
      dataSize -= processedSize;
      memmove(buffer, buffer + processedSize, dataSize);
  };
  </code></pre>
  
  <p>Is it possible to do so with a <code>std::vector</code> without losing much performance?</p>
  
  <p>EDIT:
  I have found a way to replace a raw C buffer by a <code>std::vector</code>.</p>
  
  <pre><code>std::vector&lt;BYTE&gt; vbuf;
  vbuf.reserve(MAX_SIZE); // allocated at once
  
  while (appRunning()) 
  {
      int pendingSize = GetPendingDataSize(); // from a socket
      if (pendingSize &gt; vbuf.capacity())
          pendingSize = vbuf.capacity();
  
      vbuf.resize(pendingSize);
      int recvSize = Receive(vbuf.data(), vbuf.size());
      ASSERT(recvSize &lt; vbuf.size());
      int processedSize = ProcessBuffer(vbuf.data(), vbuf.size());
      std::rotate(vbuf.begin(), vbuf.begin() + processedSize, vbuf.end());
      vbuf.resize(vbuf.size() - processedSize);
  };
  </code></pre>
  
  <p>Actually, in my practical usage, receiving data and processing data may be done in multithread. So by using vector, I do not need to manage buffer's allocation, data size and buffer capacity manually.
  Compares to the C buffer, the performance penalty here is at <code>vbuf.resize()</code> calls. But I think that penalty is insignificant.
  Any better way is appreciated.</p>