(e-mail address removed)>, (e-mail address removed)
says...
Yes. You don't have to create an empty vector, and then resize
it to get the size you want either -- you can pass the size
you want when you create it:
std::string prototype(20, ' ');
std::vector<std::string m_vec(100, prototype);
That's not quite the same thing as he wrote (although it might
be more appropriate). His code still constructs an empty
vector; it just guarantees that this vector will not require
reallocation before 100 elements are inserted.
If his goal is to avoid invalidating any iterators during a
push_back, that's all he needs. If his goal is to avoid any
further memory allocations, including for the strings, it
doesn't work. But I'm not sure why that would be a goal; if
he's using std::string, each string instance will allocate
memory separately from the vector, so creating them all up front
doesn't reduce the actual number of allocations, nor the
fragmentation. For that, he'd need some sort of fixed length
string (template< size_t length > class String?) which uses a
char data[ length ] (or a boost::array) in its implementation.