本文介绍了在 C++ 中从 std::string 转换为 char *的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用的是 VS2012/C++,我需要将 std::string 转换为 char *,但我在网上找不到任何材料来指导如何去做.
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.
任何代码示例和建议将不胜感激.
Any code examples and advice will be greatly appreciated.
推荐答案
使用
std::string bla("bla");
char* blaptr = &bla[0];
这保证自 C++11 起就有效,并且有效地适用于 C++11 之前的所有流行实现.
This is guaranteed to work since C++11, and effectively worked on all popular implementations before C++11.
如果你只需要一个 const char*
,你可以使用 std::string::c_str()
或 std::string::data()
If you need just a const char*
, you can use either std::string::c_str()
or std::string::data()
这篇关于在 C++ 中从 std::string 转换为 char *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!