Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialising a std::string from a character

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?

like image 441
Jamie Cook Avatar asked Aug 03 '09 02:08

Jamie Cook


1 Answers

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.

like image 163
Meredith L. Patterson Avatar answered Oct 01 '22 22:10

Meredith L. Patterson