简体   繁体   English

标准::环礁与 VC++

[英]std::atoll with VC++

I have been using std::atoll from cstdlib to convert a string to an int64_t with gcc.我一直在使用来自cstdlibstd::atoll将字符串转换为带有 gcc 的int64_t That function does not seem to be available on the Windows toolchain (using Visual Studio Express 2010). function 似乎在 Windows 工具链上不可用(使用 Visual Studio Express 2010)。 What is the best alternative?什么是最好的选择?

I am also interested in converting strings to uint64_t .我也有兴趣将strings转换为uint64_t Integer definitions taken from cstdint . Integer 定义取自cstdint

MSVC have _atoi64 and similar functions, see here MSVC 有 _atoi64 和类似的功能,看这里

For unsigned 64 bit types, see _strtoui64对于无符号 64 位类型,请参见_strtoui64

  • use stringstreams ( <sstream> )使用字符串流( <sstream>

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

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

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.如果您进行了性能测试并得出结论认为转换是您的瓶颈并且应该非常快地完成,并且没有现成的 function,我建议您自己编写。 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;
}

Visual Studio 2013 finally has std::atoll . Visual Studio 2013 终于有了std::atoll

Do you have strtoull available in your <cstdlib> ?您的<cstdlib>中有可用的strtoull吗? It's C99.是C99。 And C++0x should also have stoull to work directly on strings. C++ stoull也应该可以直接处理字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM