使用流实现
数字类型转为string字符串
#include<bits/stdc++.h>
using namespace std;
int main(){
int n = 123;
stringstream ss;
string str;
ss << n;
ss >> str;
cout << str;
return 0;
}
string字符串转为数字类型
#include<bits/stdc++.h>
using namespace std;
int main(){
int str = "123";
stringstream ss;
int num;
ss << str;
ss >> num;
cout << num;
return 0;
}
使用c++11中的函数直接转换
如果devcpp中方法不可用点击如下链接进行配置
https://blog.csdn.net/weixin_44635198/article/details/114404887
数字类型转为string字符串
to_string(num);
#include<bits/stdc++.h>
using namespace std;
int main(){
int n = 123456;
string str = to_string(n);
cout << str;
return 0;
}
string字符串转为数字类型
stoi(int),stol(long), stof(float), stod(double)
#include<bits/stdc++.h>
using namespace std;
int main(){
string str = "123456";
int n = stoi(str);
cout << str;
return 0;
}