Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think you should use <a href="http://en.cppreference.com/w/cpp/container/vector" rel="noreferrer"><code>std::vector::clear</code></a>:</p> <pre><code>vec.clear(); </code></pre> <hr> <p>EDIT:</p> <blockquote> <p>Doesn't clear destruct the elements held by the vector?</p> </blockquote> <p>Yes it does. It calls the destructor of every element in the vector before returning the memory. That depends on what "elements" you are storing in the vector. In the following example, I am storing the objects them selves inside the vector:</p> <pre><code>class myclass { public: ~myclass() { } ... }; std::vector&lt;myclass&gt; myvector; ... myvector.clear(); // calling clear will do the following: // 1) invoke the deconstrutor for every myclass // 2) size == 0 (the vector contained the actual objects). </code></pre> <p>If you want to share objects between different containers for example, you could store pointers to them. In this case, when <code>clear</code> is called, only pointers memory is released, the actual objects are not touched:</p> <pre><code>std::vector&lt;myclass*&gt; myvector; ... myvector.clear(); // calling clear will do: // 1) --------------- // 2) size == 0 (the vector contained "pointers" not the actual objects). </code></pre> <p>For the question in the comment, I think <code>getVector()</code> is defined like this:</p> <pre><code>std::vector&lt;myclass&gt; getVector(); </code></pre> <p>Maybe you want to return a reference:</p> <pre><code>// vector.getVector().clear() clears m_vector in this case std::vector&lt;myclass&gt;&amp; getVector(); </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