c_str()函数返回一个指向正规C字符串的指针, 内容与本string串相同.
C++中 c_str( )主要用法就是这是为了与c语言兼容,在c语言中没有string类型,故必须通过string类对象的成员函数c_str()把string 对象转换成c中的字符串样式。
c_str() 以 char* 形式传回 string 内含字符串。
char c[20];
string s="1234";
strcpy(c,s.c_str());
这样才不会出错,c_str()返回的是一个临时指针,不能对其进行操作
参考资料:
https://zhidao.baidu.com/question/104592558.html?qbl=relate_question_4&word=.str%28%29%20%CA%B2%C3%B4%D2%E2%CB%BC