c++11中为我们提供了许多非常方便的函数,可以帮助我们在整形与string类型字符串进行转换
关于Dev-c++如何使用c++11,因为本人是mac系统,使用cLion,无法安装Dev,可以在网上搜其他教程实现
整形转字符串(to_string())
to_string
函数很好的帮助了我们进行从整形到字符串的转换
#include<bits/stdc++.h>
using namespace std;
int main(){
int a=123;
string s;
// 用法为
// string变量 = to_string(整形变量)
s= to_string(a);
cout<<s;
// 输出为123
long long b=123456789123;
// 长整形也可以实现
s= to_string(b);
cout<<s;
}
字符串转整形(stoi)
#include<bits/stdc++.h>
using namespace std;
int main(){
string s="123";
int a;
// 用法为
// 整形变量=stoi(string类型变量)
a=stoi(s);
cout<<a;
// 同样长整形也能实现
long long b;
b= stoll(s);
cout<<b;
}