title: Vector re-declaration versus insertions in looping operations - C++
body: <p>I have an option to either create and destroy a vector on every call to <code>func()</code> and push elements in each iteration, as shown in Example A OR fixed the initialization and only overwrite old values in each iteration, as shown in Example B.</p>
  
  <p>Example A:</p>
  
  <pre><code>void func () 
  {
      std::vector&lt;double&gt; my_vec(5, 0.0);
      for ( int i = 0; i &lt; my_vec.size(); i++) {
          my_vec.push_back(i);
          // do something
      }
  }
  
  while (condition) {
      func();
  }
  </code></pre>
  
  <p>Example B:</p>
  
  <pre><code>void func (std::vector&lt;double&gt;&amp; my_vec) 
  {
      for ( int i = 0; i &lt; my_vec.size(); i++) {
          my_vec[i] = i;
          // do something
      }
  }
  
  while (condition) {
      std::vector&lt;double&gt; my_vec(5, 0.0);
      func(myVec);
  }
  </code></pre>
  
  <p>Which of the two would be computationally inexpensive. The size of the array won't be more than 10.</p>