字符数组换为字符串
代码:
#include<bits/stdc++.h>
using namespace std;
int main(){
char a[10]="123456789";
string s(&a[0],&a[9]);///起始位置 结束长度位置 +1
cout<<s<<endl;//可以更换数字下标运行看看
}
字符串转换为字符数组
代码:
#include<bits/stdc++.h>
using namespace std;
int main(){
string s="0123456789";
char op[15];
strncpy(op,s.c_str(),s.length()+1);///s.c_str() 字符强制转化为字符串函数
///长度必须加1,\0还占一个位置
for(int i=0;op[i];i++)
cout<<op[i]<<" ";
printf("\n");
}