1. string转const char*
1 string str ="abc"; 2 const char* charArr = str.c_str();
2. const char*转string
1 const char* charArr = "abc"; 2 string str(charArr);
3. string转char*
1 string str ="abc"; 2 char* charArr; 3 const int len = str.length(); 4 charArr =new char[len+1]; 5 strcpy(charArr, str.c_str());
4. char*转string
1 char* charArr ="abc"; 2 string str(c);
5. const char*转char*
1 const char* charArr1 ="abc"; 2 char* charArr2 = new char[100]; // 足够长 3 strcpy(charArr1, charArr2)
时间: 2024-10-09 00:40:34