一、int 转 string
#include <bits/stdc++.h>
using namespace std;
int main ()
{
//to_string()函数
int p = 526103;
string str = to_string(p);
cout << "This is a string " << str <<endl;
//字符串流stringstream
int k = 45615474; string s;
stringstream sm;
sm << k;
sm >> s;
cout << "This is a string " << s <<endl;
return 0;
}
二、string、char[ ] 转 int
#include <bits/stdc++.h>
using namespace std;
int main()
{
//C语言atoi()函数
char st[] = "17";
int p = atoi(st);
cout << "This is a number " << p << endl;
//string类型要用c_str()转化一下
string a = "100", b = "-123";
int c = atoi(a.c_str()) + atoi(b.c_str());
cout << "This is a number " << c << endl;
//字符串流stringstream
string s = "185623";
stringstream sm; int k;
sm << s;
sm >> k;
cout << "This is a number " << k << endl;
}