Initialising a std::string from a character
-
10-07-2019 - |
Question
There doesn't seem to be a standard constructor so I've taken to doing the following
void myMethod(char delimiter = ',')
{
string delimiterString = 'x';
delimiterString[0] = delimiter;
// use string version ...
}
Is there a better way to do this?
Solution
std::string
has a constructor that will do it for you:
std::string delimiterString(1, delimiter);
The 1 is a size_t
and denotes the number of repetitions of the char
argument.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow