Convert string to int C++

This question already has an answer here:

  • How to parse a string to an int in C++? 17 answers

  • In C++11 there are some nice new convert functions from std::string to a number type.

    So instead of

    atoi( str.c_str() )
    

    you can use

    std::stoi( str )
    

    where str is your number as std::string .

    There are version for all flavours of numbers: long stol(string) , float stof(string) , double stod(string) ,... see http://en.cppreference.com/w/cpp/string/basic_string/stol


    std::istringstream ss(thestring);
    ss >> thevalue;
    

    要完全正确,您需要检查错误标志。


    use the atoi function to convert the string to an integer:

    string a = "25";
    
    int b = atoi(a.c_str());
    

    http://www.cplusplus.com/reference/clibrary/cstdlib/atoi/

    链接地址: http://www.djcxy.com/p/20974.html

    上一篇: JavaScript通过了吗?

    下一篇: 将字符串转换为int C ++