当前位置 博文首页 > Aaron_Yang:C++ 字符串转换为数字(stoi,stoll,stof,stod)
将字符串转换为整型
#include<iostream>
#include<string>
using namespace std;
int main()
{
int n;
string s1="89";
n=stoi(s1);
cout<<n<<endl;
return 0;
}
将字符串转换为long long
#include<iostream>
#include<string>
using namespace std;
int main()
{
long long n;
string s1="874959387578949";
n=stoll(s1);
cout<<n<<endl;
return 0;
}
将字符串转换为float型
#include<iostream>
#include<string>
using namespace std;
int main()
{
float n;
string s1="3.1678";
n=stof(s1);
cout<<n<<endl;
return 0;
}
将字符串转换为double型
#include<iostream>
#include<string>
using namespace std;
int main()
{
double n;
string s1="3.1678";
n=stod(s1);
cout<<n<<endl;
return 0;
}
cs