STD:STOD,STD::STOF,STD::C++中的STODD

  1. std::stod(): 它将字符串转换为双精度。 语法:
    double stod( const std::string& str, std::size_t* pos = 0 );
    double stod( const std::wstring& str, std::size_t* pos = 0 );
    Return Value: return a value of type double
    Parameters
    str : the string to convert
    pos : address of an integer to store the 
    number of characters processed. This parameter can also be 
    a null pointer, in which case it is not used.
    

    // CPP program to illustrate
    // std::stod()
    #include <string>
    #include <iostream>
    int main( void )
    {
    std::string str = "y=4.4786754x+5.6" ;
    double y, x, a, b;
    y = 0;
    x = 0;
    // offset will be set to the length of
    // characters of the "value" - 1.
    std:: size_t offset = 0;
    a = std::stod(&str[2], &offset);
    b = std::stod(&str[offset + 3]);
    std::cout << b;
    return 0;
    }

    
    

    输出:

    5.6
    

    另一个例子:

    // CPP program to illustrate
    // std::stod()
    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
    string b = "5" ;
    double a = stod(b);
    int c = stoi(b);
    cout << b << " " << a << " " << c << endl;
    }

    
    

    输出:

    5 5 5
    

    如果未执行转换,将引发无效的_参数异常。如果读取的值超出可表示值的范围,则会引发“超出范围”异常。无效的idx会导致未定义的行为。

  2. std::stof: 它将字符串转换为浮点。 语法:
    float stof( const string& str, size_t* pos = 0 );
    float stof( const wstring& str, size_t* pos = 0 );
    Parameters
    str : the string to convert
    pos : address of an integer to store the number of characters processed
    This parameter can also be a null pointer, in which case it is not used.
    Return value: it returns value of type float.

    例1:

    // CPP program to illustrate
    // std::stof()
    #include <iostream>
    #include <string>
    int main()
    {
    std::string x;
    x = "20" ;
    float y = std::stof(x) + 2.5;
    std::cout << y;
    return 0;
    }

    
    

    输出:

    22.5
    

    例2:

    // CPP program to illustrate
    // std::stof()
    #include <iostream>
    #include <string>
    int main()
    {
    std::string str = "5000.5" ;
    float x = std::stof(str);
    std::cout << x;
    return 0;
    }

    
    

    输出:

    5000.5
    

    如果无法执行任何转换,将引发无效的_参数异常。

  3. std::stold: 它将字符串转换为长双精度。 语法:
    long double stold( const string& str, size_t *pos = 0 );
    long double stold (const wstring& str, size_t* pos = 0);
    Parameters : 
    str : the string to convert
    pos : address of integer to store the index of the first unconverted character.
    This parameter can also be a null pointer, in which case it is not used.
    Return value : it returns value of type long double.
    

    例1:

    // CPP program to illustrate
    // std::stold()
    #include <iostream>
    #include <string>
    int main()
    {
    std::string str = "500087" ;
    long double x = std::stold(str);
    std::cout << x;
    return 0;
    }

    
    

    输出:

    500087
    

    例2:

    // CPP program to illustrate
    // std::stold()
    #include <iostream>
    #include <string>
    int main()
    {
    std::string x;
    x = "2075" ;
    long double y = std::stof(x) + 2.5;
    std::cout << y;
    return 0;
    }

    
    

    输出:

    2077.5
    

本文由 希瓦尼·古泰尔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

null

如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享