Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 816
Chap Avatar asked Oct 21 '25 12:10

Chap


1 Answers

Something like this:

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

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

like image 120
juanchopanza Avatar answered Oct 23 '25 02:10

juanchopanza



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!