Linux 拨号vps windows公众号手机端

c++中cstring转string怎么实现

lewis 7年前 (2018-04-07) 阅读数 13 #程序编程
文章标签 c++

在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类的构造函数,我们可以轻松地实现这种转换。

版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门