初學(xué)C++损话,那些亂七八糟的string飞蛹、char*姑尺、const char *很蛋疼清焕,現(xiàn)給出各種字符串的轉(zhuǎn)換方法并蝗。
1.string轉(zhuǎn)const char*
string s = "abc";//設(shè)置一個(gè)字符串
const char* c_s = s.c_str();
2.const char*轉(zhuǎn)string
直接賦值即可
const char* c_s = "abc";
string s(c_s);
3.string轉(zhuǎn)char*
string s = "abc";
char* c;
const int len = s.length();
c = new char[len+1];
strcpy(c,s.c_str());
4.char*轉(zhuǎn)string
char* c = "abc";
string s(c);
5.const char*轉(zhuǎn)char*
const char* cpc = "abc";
char* pc = new char[100];//足夠長(zhǎng)
strcpy(pc,cpc);
6.char*轉(zhuǎn)const char*
直接賦值即可
char* pc = "abc";
const char* cpc = pc;