converting c style string to c++ style string

assassin picture assassin · Feb 11, 2010 · Viewed 38.8k times · Source

Can anyone please tell me how to convert a C style string (i.e a char* ) to a c++ style string (i.e. std::string) in a C++ program?

Thanks a lot.

Answer

John Weldon picture John Weldon · Feb 11, 2010

std::string can take a char * as a constructor parameter, and via a number of operators.

char * mystr = "asdf";
std::string mycppstr(mystr);

or for the language lawyers

const char * mystr = "asdf";
std::string mycppstr(mystr);