介尘部落

文学|音乐|休闲娱乐|计算机技术|地球科学|社会学——知识成就命运


C/C++ 中 string与const char*之间的相互转换

1. string转const 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. string转char*

 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);

6. char*转const char*

 直接赋值即可

 char* pc = "abc";

 const char* cpc = pc;
阅读全文
公众号-介尘阅读时光
赞赏支持

0 Responses to “C/C++ 中 string与const char*之间的相互转换”

Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)

×