且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将std :: string转换为char *替代

更新时间: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());