一些char [] 和char *到 string的转换知识
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/30f4961d87c450fc1590cf047cf9870c.png)
char p1[4] = {'a','a',' ','a'};
char p3[5] = "a aa";
char p4[6] = "a a a";
cout << sizeof(p3) / sizeof(p3[0]) << endl;
char *p2 = p1;
cout << strlen(p1) << endl;
cout << strlen(p3) << endl;
cout << strlen(p4) << endl;
cout << sizeof(p3) / sizeof(p3[0]) << endl;
string S1(p1,4);
cout << S1 << endl;
string S2(p2,4);
cout << S2 << endl;
string S3(p3,5);
cout << S3 << endl;
一些string 到 char *和char[]的转换知识
std::string str = "Hello Word";
到char * 采用c_str()和data()
char* p4 = (char*)str.c_str();
char* p5 = (char*)str.data();
或者
const char* p4 = str.c_str();
const char* p5 = str.data();
cout << p5 << endl;
到char [] 采用str.copy()
string str = "hmmq";
char p11[5];
str.copy((char*)p11, 4, 0);
*(p11 + 5) = '\0';
cout << p11 << endl;