如何将"char"转换为"std::string"?

How can I convert a "char" to "std::string"?

本文关键字:std string 转换 char      更新时间:2023-10-16

我试图找到如何将char数组转换为std::string,但我找不到任何东西可以做到这一点。

如果不可能,你能解释一下我怎么能读取std::string,但是通过scanf()gets()我不能

问好。

从字符数组创建字符串的最简单方法是使用一个接受指针和长度的构造函数,如下所示:

char charArray[] = {'w', 'x', 'y', 'z'};
std:string s(&charArray[1], 2); // s is "xy"
//                ^         ^
//                |         |
//    Pointer to the        |
//    starting position   Length