#include <cstdio>
#include <cstring>
#include <iostream>
#include <cstdlib>
using namespace std;
int main(){
string str = "hello world!";
int len = str.length();
char *p;
p = (char *)malloc((len)*sizeof(char));
str.copy(p,len,0);
printf("%s\n",p);
cout << p << endl;
return 0;
}
运行结果如下:
本文展示了一个使用C++进行字符串操作的例子,包括字符串长度获取、内存分配、字符串复制及输出等内容。通过这个示例,读者可以了解如何在C++中高效地处理字符串。
731

被折叠的 条评论
为什么被折叠?



