记忆方法:sto+你要的字符类型
一、stoi()
将字符串转换为整型
#include<iostream>
#include<string>
using namespace std;
int main()
{
int n;
string s1="89";
n=stoi(s1);
cout<<n<<endl;
return 0;
}
二、stoll()
将字符串转换为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;
}
三、stof()
将字符串转换为float型
#include<iostream>
#include<string>
using namespace std;
int main()
{
float n;
string s1="3.1678";
n=stof(s1);
cout<<n<<endl;
return 0;
}
四、stod()
将字符串转换为double型
#include<iostream>
#include<string>
using namespace std;
int main()
{
double n;
string s1="3.1678";
n=stod(s1);
cout<<n<<endl;
return 0;
}
本文详细介绍了C++中用于将字符串转换为数值类型的四个关键函数:stoi用于转换为int,stoll用于转换为long long,stof用于转换为float,stod用于转换为double。这些函数在处理字符串数据时非常实用,确保了数据类型的准确转换。
1万+

被折叠的 条评论
为什么被折叠?



