C ++从1个字符转换为string?

我真的没有find任何接近的答案…

相反的方式很简单,像str [0]

但我只需要投1个string…

喜欢这个:

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 return null 

所有的

 string s(1, c); std::cout << s << std::endl; 

 std::cout << string(1, c) << std::endl; 

 string s; s.push_back(c); std::cout << s << std::endl; 

为我工作。

我真的认为,铸造方法可以正常工作。 既然不行,你可以试试stringstream。 下面是一个例子:

 #include <sstream> #include <string> stringstream ss; string target; char mychar='a'; ss << mychar; ss >> target;