C++ convert strings to char arrays

…衆ロ難τιáo~ 提交于 2019-12-06 02:05:04

Just don't do it, because you don't need to do it.

You could use the c_str() function to convert the std::string to a char array, but in your case it's just unnecessary. For the case of finding elements (which I believe is what you need) you can use the string's operator[] and treat it as if it was an ordinary array.

You can use c_str member of the std::string and strcpy function to copy the data into the arrays:

//array=s;
strcpy(array, s.c_str());
//array2=s2;
strcpy(array2, s2.c_str());

However, you don't have to do that unless you must pass non-constant char arrays around, because std::strings look and feel much like character arrays, except they are much more flexible. Unlike character arrays, strings will grow to the right size as needed, provide methods for safe searching and manipulation, and so on.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!