[英]Converting from std::string to char * in C++
I'm using VS2012/C++, I need to convert a std::string to char * and I can't find any material online giving any guidance on how to go about doing it. 我正在使用VS2012 / C ++,我需要将std :: string转换为char *,我在网上找不到任何材料给出任何关于如何去做的指导。
Any code examples and advice will be greatly appreciated. 任何代码示例和建议将不胜感激。
Use 采用
std::string bla("bla");
char* blaptr = &bla[0];
This is guaranteed to work since C++11, and effectively worked on all popular implementations before C++11. 这保证在C ++ 11之后起作用,并且在C ++ 11之前有效地适用于所有流行的实现。
If you need just a const char*
, you can use either std::string::c_str()
or std::string::data()
如果你只需要一个const char*
,你可以使用std::string::c_str()
或std::string::data()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.