char转string
误区
无法使用to_string()方法
char c = 'c';
cout << to_string(c) == "c" ? true : false << endl;
这样是错误的to_string是将char字符的ASCII码值转换成了字符串
方法一
定义一个空string,调用append()方法
char c = 'c';
string s = "";
s.append(1, c);
cout << s == "c" ? true : false << endl;
方法二
使用stringstream,注意添加相应头文件
#include <sstream>
stringstream stream;
char c = 'c';
stream << c;
cout << stream.str() == "c" ? true : false;
方法三
使用string构造函数
char c = 'c';
string s(1, c);
cout << s == "c" ? true : false << endl;