convert a char* to std::string

后端 未结 11 1017
萌比男神i
萌比男神i 2020-12-02 04:08

I need to use an std::string to store data retrieved by fgets(). To do this I need to convert the char* return value from fgets(

11条回答
  •  星月不相逢
    2020-12-02 04:23

    std::string has a constructor for this:

    const char *s = "Hello, World!";
    std::string str(s);
    

    Note that this construct deep copies the character list at s and s should not be nullptr, or else behavior is undefined.

提交回复
热议问题