C++ char to string 方法

1. 使用string()构造函数方法

1   //method 1: the constructor of string()
2     char c = 'F';
3     string s = string(1, c);
4     cout << s ;

 

2. 使用stringstream字符流

1  //method 2: stringstream
2     char c1 = 'F';
3     stringstream ss;
4     ss << c1;
5     string s2;
6     ss >> s2;
7     cout << s2;

 

3. 使用springf()函数

1     //method 3: sprintf()
2     char c2 = 'F';
3     char s3[] ={};
4     sprintf(s3, "%c", c2);
5     string s4 = string(s3);
6     cout << s4;

 

水滴石穿,笨鸟先飞!

 

posted @ 2020-02-28 13:44  SheepCore  阅读(4257)  评论(0编辑  收藏  举报