1、可以用字符赋值,但不能用字符初始化
string a = 'a';//错误
string a;
a = 'a';//正确
2、转换成C语言式char* 字符串
成员函数c_str()
string s1("hello word");
printf("%s\n",s1.c_str());//s1.c_str()返回传统的const char* 类型字符串,且该字符串以'\0'结尾。
输出:
hello word
3、转换成C语言式char* 字符串
成员函数data()
string s1("hello word");
const char* p1=s1.data();
for(int i=0;i<s1.length();i++)
printf("%c",*(p1+i));//s1.data()返回一个char* 类型的字符串(不是const char*),对s1的修改可能会使p1出错。