我已经找到并阅读了一些类似的问题,但遗憾的是我无法在其中找到解决方案。我只想将字符串转换为字符串中字母大小的字符数组。例子:
string test = "hello";
字符数组:
testArr[0]: 'h';
testArr[1]: 'e';
testArr[2]: 'l';
testArr[3]: 'l';
testArr[4]: 'o';
在 Java 中这很容易,但在 C++ 中似乎很复杂。
类 string 有一个c_str()
返回常量字符串的成员函数。很简单:
std::string test = "hello";
char* cstrTest = new char[test.length() + 1];
strcpy(cstrTest, test.c_str());
std::cout << cstrTest << std::endl;
delete[] cstrTest;
如果您只想更改常量字符串而不更改它(const),只需:
const char* cTest = test.c_str();
cout << cTest << endl;
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句