C++ char*,const char*,string的相互转换代码实例
程序员文章站
2022-04-03 16:54:36
1.string转const char*
string s ="abc";
constchar* c_s = s.c_str();
2.const cha...
1.string转const char*
string s ="abc";
constchar* c_s = s.c_str();
2.const char*转string
直接赋值即可
constchar* c_s ="abc";
string s(c_s);
3.string转char*
string s ="abc";
char* c;
constint len = s.length();
c =newchar[len+1];
strcpy(c,s.c_str());
4.char*转string
char* c ="abc";
string s(c);
5.const char*转char*
constchar* cpc ="abc";
char* pc =newchar[100];//足够长
strcpy(pc,c
上一篇: 算法竞赛_子序列之和_C++