[英]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? 有一个更好的方法吗?
std::string
has a constructor that will do it for you: std::string
有一个构造函数,可以为你完成:
std::string delimiterString(1, delimiter);
The 1 is a size_t
and denotes the number of repetitions of the char
argument. 1是
size_t
,表示char
参数的重复次数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.