Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You would not normally explicitly <code>resize</code> a vector at all -- neither in the function call nor before you call the function. <code>vector</code> manages its own memory usage, and grows as needed to accommodate new elements.</p> <p>There are circumstances where you will be adding mane elements and do not want to suffer numerous re-allocations of the vector for performance reasons. This will normally occur only in the "hot path" of you application, and not for every application. In these relatively rare scenarios, the function that adds elements should be responsible for enusring there is enough capacity in the vector. To do this, you do not use <code>resize</code>, but <code>reserve</code>. Both will reallocate the <code>vector</code> if necesarry (and consequently invalidate iterators, so beware), but <code>resize</code> actually changes botht the <code>size()</code> and <code>capacity()</code> of the <code>vector</code>, whereas <code>reserve</code> only changes the <code>capacity()</code>.</p> <p>To understand the difference:</p> <pre><code>vector &lt;int&gt; v; v.reserve (10); copy (v.begin(), v.end(), ostream_iterator (cout, ",")); </code></pre> <p>The above will output nothing because there is nothing "in" the <code>vector</code>, even though there is room for 10 elements. However:</p> <pre><code>vector &lt;int&gt; v; v.resize (10); copy (v.begin(), v.end(), ostream_iterator (cout, ",")); </code></pre> <p>This code will output 10 default-constructed <code>int</code>s. There are 10 elements in the array. If you are <code>resizing()</code> a <code>vector</code> simply to make room for new elements, <code>resize()</code> probably isn't what you want to do.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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