I need a way to create a string of n chars. In this case ascii value zero.
I know I can do it by calling the constructor:
string sTemp(125000, ‘a’);
but I would like to reuse sTemp in many places and fill it with different lengths.
I am calling a library that takes a string pointer and length as an argument and fills the string with bytes. (I know that technically string is not contiguous, but for all intents and purposes it is, and will likly become the standard soon). I do NOT want to use a vector.
is there some clever way to call the constructor again after the string has been created?
The
stringclass provides the methodassignto assign a given string a new value. The signatures areCiting source: cplusplus.com (I recommend this website because it gives you a very elaborated reference of the C++ standard libraries.)
I think you’re looking for something like the fifth one of these functions:
nspecifies the desired length of your string andcthe character filled into this string. For example if you writeyour string will be solely filled with 10 b’s.
I originally suggested to use the STL Algorithm
std::fillbut thus your string length stays unchanged. The methodstring::resizeprovides a way to change the string’s size and fills the appended characters with a given value — but only the appended ones are set. Finallystring::assignstays the best approach!