Converting a C-style string to a C++ std::string
问题 What is the best way to convert a C-style string to a C++ std::string ? In the past I've done it using stringstream s. Is there a better way? 回答1: C++ strings have a constructor that lets you construct a std::string directly from a C-style string: const char* myStr = "This is a C string!"; std::string myCppString = myStr; Or, alternatively: std::string myCppString = "This is a C string!"; As @TrevorHickey notes in the comments, be careful to make sure that the pointer you're initializing the