Converting a C-style string to a C++ std::string

后端 未结 6 1519
梦如初夏
梦如初夏 2020-12-14 00:42

What is the best way to convert a C-style string to a C++ std::string? In the past I\'ve done it using stringstreams. Is there a better way?

6条回答
  •  悲哀的现实
    2020-12-14 01:09

    If you mean char* to std::string, you can use the constructor.

    char* a;
    std::string s(a);
    

    Or if the string s already exist, simply write this:

    s=std::string(a);
    

提交回复
热议问题