在C++中,可以使用std::string构造函数将C字符串(以空字符结尾的字符数组)转换为std::string对象
#include<iostream>
#include<string>
int main() {
// 定义一个C字符串
const char* c_str = "Hello, World!";
// 使用C字符串初始化std::string对象
std::string cpp_str(c_str);
// 输出转换后的std::string对象
std::cout << "C++ string: " << cpp_str<< std::endl;
return 0;
}
在这个示例中,我们首先定义了一个C字符串c_str。然后,我们使用std::string构造函数将其转换为std::string对象cpp_str。最后,我们输出转换后的std::string对象。
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: c++中emit的使用方法是什么