更新时间:2022-05-31 06:01:21
你可以得到一个 const char *
到使用 c_str
的字符串:
You can get a const char*
to the string using c_str
:
std::string str = "foo bar" ;
const char *ptr = str.c_str() ;
如果你只需要一个char *,你必须复制一份。做:
If you need just a char* you have to make a copy, e.g. doing:
char *cpy = new char[str.size()+1] ;
strcpy(cpy, str.c_str());