char*,const char*和string的相互轉換

1. string轉const char*spa

   string s = "abc";orm

   const char* c_s = s.c_str();string

2. const char*轉stringit

   直接賦值便可im

   const char* c_s = "abc";margin

   string s(c_s);top

3. string轉char*word

   string s = "abc";di

   char* c;co

   const int len = s.length();

   c = new char[len+1];

   strcpy(c,s.c_str());

4. char*轉string

   char* c = "abc";

   string s(c);

5. const char*轉char*

   const char* cpc = "abc";

   char* pc = new char[100];//足夠長

   strcpy(pc,cpc);

6. char*轉const char*

   直接賦值便可

   char* pc = "abc";

   const char* cpc = pc;

相關文章
相關標籤/搜索