在用C++刷题的时候,经常会碰到字符数组和字符串的相互转换,故对此进行简单整理。
#include<iostream>
#include<>
using namespace std;
int main()
{
char str[]={'a','b','c','d','e'};
//一维字符数组转化字符串
string tempStr1,tempStr2;
//法一
for(int i=0;i<strlen(str);i++)
{
tempStr1+=str[i];
}
//法二
tempStr2=str;
cout<<tempStr1<<" "<<tempStr2<<endl;
//字符串转字符数组
string tempStr3="I LOVE CHINA";
char temp[20];
int i=0;
for(i;i<();i++)
{
temp[i]=tempStr3[i];
}
temp[i]='\0';
cout<<temp<<endl;
}