C++字符串相互转换

转自cs_wu原文 C++ char*,const char*,string的相互转换

1. stringconst char*

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

 

2. const char*string

const char* c_s ="abc";
string s(c_s);

  

3. stringchar*

string s ="abc";
char* c;
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);

 

posted @ 2019-10-21 18:56  wenglabs  阅读(1065)  评论(0编辑  收藏  举报