How can I convert a std::string to int?

后端 未结 19 1418
梦毁少年i
梦毁少年i 2020-11-22 02:10

Just have a quick question. I\'ve looked around the internet quite a bit and I\'ve found a few solutions but none of them have worked yet. Looking at converting a string to

19条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-22 02:44

    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

提交回复
热议问题