std::atoll with VC++

▼魔方 西西 提交于 2019-12-06 21:44:33

问题


I have been using std::atoll from cstdlib to convert a string to an int64_t with gcc. That function does not seem to be available on the Windows toolchain (using Visual Studio Express 2010). What is the best alternative?

I am also interested in converting strings to uint64_t. Integer definitions taken from cstdint.


回答1:


MSVC have _atoi64 and similar functions, see here

For unsigned 64 bit types, see _strtoui64




回答2:


  • use stringstreams (<sstream>)

    std::string numStr = "12344444423223";
    std::istringstream iss(numStr);
    long long num;
    iss>>num;
    
  • use boost lexical_cast (boost/lexical_cast.hpp)

     std::string numStr = "12344444423223";
     long long num = boost::lexical_cast<long long>(numStr);
    



回答3:


If you have run a performance test and concluded that the conversion is your bottleneck and should be done really fast, and there's no ready function, I suggest you write your own. here's a sample that works really fast but has no error checking and deals with only positive numbers.

long long convert(const char* s)
{
    long long ret = 0;
    while(s != NULL)
    {
       ret*=10; //you can get perverted and write ret = (ret << 3) + (ret << 1) 
       ret += *s++ - '0';
    }
    return ret;
}



回答4:


Do you have strtoull available in your <cstdlib>? It's C99. And C++0x should also have stoull to work directly on strings.




回答5:


Visual Studio 2013 finally has std::atoll.



来源:https://stackoverflow.com/questions/6610548/stdatoll-with-vc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!