title: How to efficiently copy a std::string into a vector
body: <p>I have a string<br></p>
  
  <pre><code>std::string s = "Stack Overflow";
  </code></pre>
  
  <p>That I need to copy into a vector.
  This is how I am doing it<br></p>
  
  <pre><code>std::vector&lt;char&gt; v;
  v.reserve(s.length()+1);
  for(std::string::const_iterator it = s.begin(); it != s.end(); ++it)
  {
      v.push_back( *it );
  }
  v.push_back( '\0' );
  </code></pre>
  
  <p>But I hear range operation are more efficient. So I am thinking something like this<br></p>
  
  <pre><code>std::vector&lt;char&gt; v( s.begin(), s.end());
  v.push_back('\0');
  </code></pre>
  
  <p>But is this better in this case? What about the potential re-allocation when inserting '\0'?<br>
  Another approach I am thinking is this<br></p>
  
  <pre><code>std::vector&lt;char&gt; v(s.length()+1);
  std::strcpy(&amp;v[0],s.c_str());
  </code></pre>
  
  <p>Perhaps fast but potentially unsafe? <br>
  EDIT<br>
  Has to be a null terminated string that can be used ( read/write ) inside a C function<br></p>