string类型有一个方法叫c_str() 就是返回C风格字符串的。 这个函数实际上返回的是一个指向常量的字符指针。 指向的就是string首字符的地址。
[quote=引用 13 楼 starytx 的回复:] char[]->string,直接=或者string构造函数里写char[]就行 string->char[],需要保证char数组大小放得下string里的内容,string.length()可以获得string中的字符串长度,用strcpy来拷贝字符串就行了,结尾的\0需要加上
[quote=引用 4 楼 u010165006 的回复:] [quote=引用 3 楼 oracleperl 的回复:] [quote=引用 2 楼 u010165006 的回复:] strcpy(mm,qq.c_str()); string构造函数(0结尾C字符串)。
#include <iostream> #include <string> using namespace std; int main() { char a[10]="hello"; string s; s=string(a); s+=" world"; cout<<s; return 0; }
[quote=引用 3 楼 oracleperl 的回复:] [quote=引用 2 楼 u010165006 的回复:] strcpy(mm,qq.c_str()); string构造函数(0结尾C字符串)。
char[]->string,直接=或者string构造函数里写char[]就行 string->char[],需要保证char数组大小放得下string里的内容,string.length()可以获得string中的字符串长度,用strcpy来拷贝字符串就行了,结尾的\0需要加上
[quote=引用 10 楼 zhao4zhong1 的回复:] 万一string里面有'\0'咋办?!
万一string里面有'\0'咋办?!
[quote=引用 2 楼 u010165006 的回复:] strcpy(mm,qq.c_str()); string构造函数(0结尾C字符串)。
strcpy(mm,qq.c_str()); string构造函数(0结尾C字符串)。
64,646
社区成员
250,476
社区内容
加载中
试试用AI创作助手写篇文章吧