title: How can I initialize a std::vector with a size parameter and have each object constructed independently?
body: <p>I like creating vectors with a given size and value, for example like this:</p>
  
  <pre><code>std::vector&lt;std::string&gt; names(10);
  </code></pre>
  
  <p>However, this a few times this led to unexpected results. For example in the following code each <code>UniqueNumber</code> turns out to have the same value:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;vector&gt;
  
  
  struct UniqueNumber
  {
      UniqueNumber() : mValue(sInstanceCount++)
      {
      }
  
      inline unsigned int value() const
      {
          return mValue;
      }
  
  private:
      static unsigned int sInstanceCount;
      unsigned int mValue;
  };
  
  
  int UniqueNumber::sInstanceCount(0);
  
  
  int main()
  {
      std::vector&lt;UniqueNumber&gt; numbers(10);
      for (size_t i = 0; i &lt; numbers.size(); ++i)
      {
          std::cout &lt;&lt; numbers[i].value() &lt;&lt; " ";
      }
  }
  </code></pre>
  
  <p>Console output:</p>
  
  <pre><code>0 0 0 0 0 0 0 0 0 0
  </code></pre>
  
  <p>It does make sense when looking at std::vector's constructor:</p>
  
  <pre><code>explicit vector(size_type __n,
                  const value_type&amp; __value = value_type(),
                  const allocator_type&amp; __a = allocator_type());
  </code></pre>
  
  <p>Apparently the vector was initialized with copies of the same object.</p>
  
  <p>Is there also a way to have each object default constructed?</p>