我只需要将1个char转换为string。相反的方式非常简单,就像str[0]一样。
以下内容对我不起作用:
char c = 34;
string(1,c);
//this doesn't work, the string is always empty.
string s(c);
//also doesn't work.
boost::lexical_cast<string>((int)c);
//also doesn't work.发布于 2013-06-20 05:32:27
所有
std::string s(1, c); std::cout << s << std::endl;和
std::cout << std::string(1, c) << std::endl;和
std::string s; s.push_back(c); std::cout << s << std::endl;对我很管用。
发布于 2013-06-20 05:28:58
老实说,我认为造型方法会工作得很好。既然它不支持,你可以尝试stringstream。示例如下:
#include <sstream>
#include <string>
std::stringstream ss;
std::string target;
char mychar = 'a';
ss << mychar;
ss >> target;发布于 2020-10-18 05:33:53
无论您拥有多少个char变量,此解决方案都将起作用:
char c1 = 'z';
char c2 = 'w';
std::string s1{c1};
std::string s12{c1, c2};https://stackoverflow.com/questions/17201590
复制相似问题