C++ instantiating std::vector<std::string> with fixed number of empty strings

c++, default, initialization, vector

Solution

Something like this:

std::vector<std::string> v(N);

where `N` is the number of strings. This creates a vector with `N` empty strings.

Problem

I'm developing a class for building records (with a fixed number of fields). My public methods allow the user to insert individual values by index. There's no requirement that the user fill in all fields -- so I'd like to preallocate the vector that represents the record to the exact size, with every field initialized to the empty string. Is there a way to do this more easily than with a push-back loop?

Original source

Related problems