【c++基础】字符数组和string相互转换

字符数组转化成string类型
char ch [] = "ABCDEFG";
string str(ch);//也可string str = ch;
或者
char ch [] = "ABCDEFG";
string str;
str = ch;//在原有基础上添加可以用str += ch;

将string类型转换为字符数组
char buf[10];
string str("ABCDEFG");
length = str.copy(buf, 9);
buf[length] = '';
或者
char buf[10];
string str("ABCDEFG");
strcpy(buf, str.c_str());//strncpy(buf, str.c_str(), 10);

参考

1.

http://www.cnblogs.com/fnlingnzb-learner/p/6369234.html

原文地址:https://www.cnblogs.com/happyamyhope/p/9923588.html