c++中cstring转string怎么实现
导读:在C++中,可以使用std::string类的构造函数来实现将cstring转换为string。下面是一个简单的示例: #include <iostream> #include <cstring> #include...
在C++中,可以使用std::string
类的构造函数来实现将cstring
转换为string
。下面是一个简单的示例:
#include iostream>
#include cstring>
#include string>
int main() {
const char* cstr = "Hello, World!";
std::string str(cstr);
std::cout "Converted string: " str std::endl;
return 0;
}
在上面的示例中,我们将一个const char*
类型的cstring
转换为std::string
类型的string
,并将其输出到控制台。通过使用std::string
类的构造函数,我们可以轻松地实现这种转换。
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: c++中cstring转string怎么实现
本文地址: https://pptw.com/jishu/669791.html