1. 程式人生 > >【轉載】atoi() 函式 與 itoa()函式

【轉載】atoi() 函式 與 itoa()函式

ANSI C 規範定義了 stof()atoi()atol()strtod()strtol()strtoul() 共6個可以將字串轉換為數字的函式,大家可以對比學習。另外在 C99 / C++11 規範中又新增了5個函式,分別是 atoll()、strtof()、strtold()、strtoll()、strtoull(),

stoi函式預設要求輸入的引數字串是符合int範圍的[-2147483648, 2147483647],否則會runtime error。 atoi函式則不做範圍檢查,若超過int範圍,則顯示-2147483648(溢位下界)或者2147483647(溢位上界)。 stoi標頭檔案:<string>,c++函式 atoi標頭檔案:<cstdlib>,c函式 atoi()的引數是 const char* ,因此對於一個字串str我們必須呼叫 c_str()的方法把這個string轉換成 const char*型別的, 而stoi()的引數是const string*,不需要轉化為 const char*;