Initializing a std :: string from a character

There seems to be no standard constructor, so I did the following

void myMethod(char delimiter = ',')
{
    string delimiterString = 'x';
    delimiterString[0] = delimiter;
    // use string version ...
}

Is there a better way to do this?

+3
source share
1 answer

std::string has a constructor that will do this for you:

std::string delimiterString(1, delimiter);

1 is size_tand denotes the number of times the argument is repeated char.

+17
source

Source: https://habr.com/ru/post/1714183/


All Articles