2011-07-07 21 views
9

我一直在使用cstdlibstd::atoll將字符串轉換爲使用gcc的int64_t。該功能似乎不可用在Windows工具鏈上(使用Visual Studio Express 2010)。什麼是最好的選擇?std ::使用VC++的環礁

我也對strings轉換爲uint64_t感興趣。整數的定義取自cstdint

+0

看來這個問題在VS2013中修復http://connect.microsoft.com/VisualStudio/feedback/details/752386/std-atoll-not-found – javapowered

回答

8

MSVC有_atoi64和類似的功能,看到here

對於無符號的64位類型,請參閱_strtoui64

+0

乾杯,謝謝。 – Cookie

+0

對於其他人,作爲參考,這似乎沒有與uint64_t等效,我切換到使用int64_t(從第三方庫轉換它) – Cookie

+0

@Cookie,添加到類似函數的鏈接,用於無符號64位類型 – nos

5
  • 使用stringstreams(<sstream>

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

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

我寧願不是因爲性能原因 – Cookie

+5

@Cookie:你運行性能測試並發現將字符串轉換爲數字是您的瓶頸? –

+0

valgrind告訴我,例如strtod是總時間的8%。 atol有點落後於4%。我實際上正在重寫strtod以擺脫這8%。 stringstreams比atol慢得多。而lexical_cast是最慢的。大部分時間用於解析一些100兆csv。 – Cookie

2

如果已經運行了性能測試,並得出結論,轉換爲你瓶頸,應該做得非常快,我沒有準備好的功能,我建議你寫你自己的。 這是一個快速運行的示例,但沒有錯誤檢查並只處理正數。

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; 
} 
+0

謝謝阿門,我只是可能。這是非常類似於http://stackoverflow.com/questions/5830868/c-stringstream-is-too-slow-how-to-speed-up雙打。 – Cookie

1

你有你的<cstdlib>strtoull可用?這是C99。而C++ 0x也應該有stoull直接在字符串上工作。

+0

不幸的是,MSVC不支持C99。 – nos

1

的Visual Studio 2013終於有了std::atoll

相關問題