How to clear a vector but keeping its capacity?
<p>I am trying to fix some code that uses vectors a lot and there are some loops that look like this:</p>

<pre><code>for (int t=0;t&lt;T;t++){    
    std::vector&lt;double&gt; vect;
    for (int i=0;i&lt;MAX;i++){
        double value;
        vect.push_back(value);
    }
    /*....*/
}
</code></pre>

<p>I know more or less how to improve this by reusing the same vector for the outer iterations, but while doing this I found that when calling <code>std::vector::clear</code> <a href="http://www.cplusplus.com/reference/vector/vector/clear/" rel="noreferrer">"the vector capacity is not guaranteed to change"</a>, while I was actually hoping that the capacity would be guaranteed to not change. Maybe I am just misunderstanding what is written on cplusplus.com. However, my question is:</p>

<p><strong>How can I clear a vector without changing its capacity?</strong></p>

<p>Should I call <code>reserve</code> after <code>clear</code> to make sure the capacity will be the same? </p>

<p><strong>PS:</strong> just to be clear, I want to rewrite above code to</p>

<pre><code>std::vector&lt;double&gt; vect;
vect.reserve(MAX);
for (int t=0;t&lt;T;t++){    
    for (int i=0;i&lt;MAX;i++){
        double value;
        vect.push_back(value);
    }
    /*....*/
    vect.clear();
}
</code></pre>

<p>ie. I still want to fill it via <code>push_back</code> and I am worried about the <code>clear()</code> changing the capacity of the vector.</p>