C ++ convert from 1 char to string?

I really did not find an answer that is close ...

the opposite way is pretty simple as str [0]

But I need to drop only 1 char per line ...

like this:

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 
+64
c ++ casting
Jun 19 '13 at 21:22
source share
2 answers

All

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

and

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

and

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

worked for me.

+105
Jun 19 '13 at 21:32
source share

I honestly thought that the casting method would work fine. Since you cannot try stringstream. The following is an example:

 #include <sstream> #include <string> stringstream ss; string target; char mychar='a'; ss << mychar; ss >> target; 
+5
Jun 19 '13 at 21:28
source share



All Articles