目录
有用的话请给个赞和收藏谢谢
char* -> string:
使用构造函数(转换函数)即可(方法较多,不多展示):
int main()
{
char str1[30];
strcpy(str1,"hello,world");
string str2(str1);
//string str2 = str1 //转换函数
cout << "str2=" << str2 <<endl;
}
str2=hello,world
string->char* :
int main()
{
char str1[30];
int len1 = sizeof(str1);
memset(str1,0,len1);
string str2 = "hello world";
int len2 = str2.length();
if(len1<len2)
{
str2.copy(str1,len1);
str1[len1] = 0;
}
else
{
str2.copy(str1,len2); //将len2个字符复制到str1
str1[len2] = 0;
}
cout << "str1=" << str1 <<endl;
}
str1=hello world