2013-06-27 90 views
1

考慮下面的例子:std :: strtoul等效於C++標準庫嗎?

#include <iostream> 
#include <clocale> 
#include <cstdlib> 
#include <string> 
int main() 
{ 
    std::setlocale(LC_ALL, "en_US.utf8"); 
    std::string s = "03A0"; 
    wchar_t wstr = std::strtoul(s.c_str(), nullptr, 16); 
    std::wcout << wstr; 
} 

此輸出ΠColiru

問題

std::strtoul,距離<cstdlib>。我用它完全沒問題,但是我想知道上面的例子是否可能只使用C++標準庫(可能是stringstreams)?

另請注意,字符串上沒有指定十六進制的優先。

回答

6

當然,std::stoul

​​

的主要區別是,它可以拋出錯誤異常。

+0

啊,啊,我完全忘了那些。它看起來像[MSVC2012](http://msdn.microsoft.com/en-us/library/ee404871.aspx)支持它們,非常棒。 –

+0

@JesseGood,Yup,不過要小心,因爲大多數'to_string'調用都會模糊不清。 – chris