Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The general rule with STL containers is that they make copies of their contents. With C++11, there are special ways of moving elements into a container (for example, the <a href="http://msdn.microsoft.com/en-us/library/dd647620%28VS.100%29.aspx" rel="noreferrer"><code>emplace_back()</code></a> member function of <code>std::vector</code>), but in your example, the elements are <code>char</code> objects, so you are still going to copy each of the <code>size</code> <code>char</code> objects.</p> <p>Think of a <code>std::vector</code> as a wrapper of a pointer to an array together with the length of the array. The closest equivalent of "casting a <code>char *</code> to a <code>std::vector&lt;char&gt;</code>" is to swap out the vector's pointer and length with a given pointer and length however the length is specified (two possibilities are a pointer to one past the end element or a <code>size_t</code> value). There is no standard member function of <code>std::vector</code> that you can use to swap its internal data with a given pointer and length.</p> <p>This is for good reason, though. <code>std::vector</code> implements ownership semantics for every element that it contains. Its underlying array is allocated with <em>some</em> allocator (the second template parameter), which is <code>std::allocator</code> by default. If you were allowed to swap out the internal members, then you would need to ensure that the same set of heap allocation routines were used. Also, your STL implementation would need to fix the method of storing "length" of the vector rather than leaving this detail unspecified. In the world of OOP, specifying more details than necessary is generally frowned upon because it can lead to higher coupling.</p> <p>But, assume that such a member function exists for your STL implementation. In your example, you simply do not know how <code>data</code> was allocated, so you could inadvertently give a <code>std::vector</code> a pointer to heap memory that was not allocated with the expected allocator. For example, <code>data</code> could have been allocated with <code>malloc</code> whereas the vector could be freeing the memory with <code>delete</code>. Using mismatched allocation and deallocation routines leads to Undefined Behavior. You might require that <code>someFunction()</code> only accept data allocated with a particular allocation routine, but this is specifying more details than necessary again.</p> <p>Hopefully I have made my case that a <code>std::vector</code> member function that swaps out the internal data members is not a good idea. If you really need a <code>std::vector&lt;char&gt;</code> from <code>data</code> and <code>size</code>, you should construct one with:</p> <pre><code>std::vector&lt;char&gt; vec(data, data + size); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload