convert a char* to std::string

后端 未结 11 994
萌比男神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:30

    Not sure why no one besides Erik mentioned this, but according to this page, the assignment operator works just fine. No need to use a constructor, .assign(), or .append().

    std::string mystring;
    mystring = "This is a test!";   // Assign C string to std:string directly
    std::cout << mystring << '\n';
    

提交回复
热议问题