<p>As a function argument I get a <code>vector&lt;double&gt;&amp; vec</code> (an output vector, hence non-const) with unknown length and values. I want to initialise this vector to a specific length n with all zeroes.</p>

<p>This will work </p>

<pre><code>vec.clear();
vec.resize( n, 0.0 );
</code></pre>

<p>And this will work as well:</p>

<pre><code>vec.resize( n );
vec.assign( n, 0.0 );
</code></pre>

<p>Is the second more efficient (because no memory deallocation/allocation is involved)? Is there a more efficient way to do this? </p>

```
```
std::vector<double>(n).swap(vec);
```
```
This has the advantage of actually compacting your vector too. (In your first example,
```
clear()
```
does not guarantee to compact your vector.)