当前位置 博文首页 > Aaron_Yang:C++ 字符串转换为数字(stoi,stoll,stof,stod)

    Aaron_Yang:C++ 字符串转换为数字(stoi,stoll,stof,stod)

    作者:[db:作者] 时间:2021-07-17 15:49

    记忆方法: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;
    } 
    
    cs
    下一篇:没有了