Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A <code>vector</code> has capacity and it has size. The capacity is the number of elements for which memory has been allocated. Size is the number of elements which are actually in the vector. A <code>vector</code> is empty when its size is 0. So, <code>size()</code> returns 0 and <code>empty()</code> returns <code>true</code>. That says nothing about the capacity of the <code>vector</code> at that point (that would depend on things like the number of insertions and erasures that have been done to the <code>vector</code> since it was created). <code>capacity()</code> will tell you the current capacity - that is the number of elements that the <code>vector</code> can hold before it will have to reallocate its internal storage in order to hold more.</p> <p>So, when you construct a <code>vector</code>, it has a certain size and a certain capacity. A default-constructed <code>vector</code> will have a size of zero and an implementation-defined capacity. You can insert elements into the <code>vector</code> freely without worrying about whether the <code>vector</code> is large enough - up to <code>max_size()</code> - <code>max_size()</code> being the maximum capacity/size that a <code>vector</code> can have on that system (typically large enough not to worry about). Each time that you insert an item into the <code>vector</code>, if it has sufficient capacity, then no memory-allocation is going to be allocated to the <code>vector</code>. However, if inserting that element would exceed the capacity of the <code>vector</code>, then the <code>vector</code>'s memory is internally re-allocated so that it has enough capacity to hold the new element as well as an implementation-defined number of new elements (typically, the <code>vector</code> will probably double in capacity) and that element is inserted into the vector. This happens without you having to worry about increasing the <code>vector</code>'s capacity. And it happens in <a href="https://stackoverflow.com/questions/200384/constant-amortized-time">constant amortized time</a>, so you don't generally need to worry about it being a performance problem.</p> <p>If you do find that you're adding to a <code>vector</code> often enough that many reallocations occur, and it's a performance problem, then you can call <code>reserve()</code> which will set the capacity to at least the given value. Typically, you'd do this when you have a very good idea of how many elements your <code>vector</code> is likely to hold. However, unless you know that it's going to a performance issue, then it's probably a bad idea. It's just going to complicate your code. And <a href="https://stackoverflow.com/questions/200384/constant-amortized-time">constant amortized time</a> will generally be good enough to avoid performance issues.</p> <p>You can also construct a <code>vector</code> with a given number of default-constructed elements as you mentioned, but unless you really want those elements, then that would be a bad idea. <code>vector</code> is supposed to make it so that you don't have to worry about reallocating the container when you insert elements into it (like you would have to with an array), and default-constructing elements in it for the purposes of allocating memory is defeating that. If you really want to do that, use <code>reserve()</code>. But again, don't bother with <code>reserve()</code> unless you're certain that it's going to improve performance. And as was pointed out in another answer, if you're inserting elements into the <code>vector</code> based on user input, then odds are that the time cost of the I/O will far exceed the time cost in reallocating memory for the <code>vector</code> on those relatively rare occasions when it runs out of capacity.</p> <p>Capacity-related functions:</p> <pre><code>capacity() // Returns the number of elements that the vector can hold reserve() // Sets the minimum capacity of the vector. </code></pre> <p>Size-related functions:</p> <pre><code>clear() // Removes all elements from the vector. empty() // Returns true if the vector has no elements. resize() // Changes the size of the vector. size() // Returns the number of items in the vector. </code></pre>
    singulars
    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.
 

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