Dlang 将 const char* 转换为 string 的多种方法
Dlang 将 const char* 转换为 string 的多种方法
在 Dlang 中,将 const char* 转换为 string 有多种方法,以下是四种常用的方法:
- 使用 std::string 的构造函数
const char* str = 'hello';
std::string s(str);
- 使用 std::string 的 assign 函数
const char* str = 'hello';
std::string s;
s.assign(str);
- 使用 std::string 的 operator=
const char* str = 'hello';
std::string s;
s = str;
- 使用 std::stringstream
const char* str = 'hello';
std::stringstream ss;
ss << str;
std::string s = ss.str();
以上四种方法都可以实现将 const char* 转换为 string 的目的,您可以根据自己的需求选择合适的方法。
原文地址: https://www.cveoy.top/t/topic/ozwb 著作权归作者所有。请勿转载和采集!