atoi stoi和strtol的使用

2021-08-18 21:24:00 字數 1571 閱讀 6585

** :

1、atoi

將string字串轉換為int型別,只能轉換為十進位制;atoi函式不會對string字串進行範圍檢查[-2147483648,2147483647],超過這個界限,不會報錯,只會進行相應的轉換,遇到非法字元會停止,不會報錯;標頭檔案為cstdlib

[html]view plain

copy

#include

<

iostream

>

#include<

cstdlib

>

#include<

string

>

using namespace std;  

void atoiint1()  

void atoiint2()  

void atoiint3()  

int main()   

輸出:111111

111-2147483648

2、stoi

將string字串轉換為int型別,只能轉換為十進位制;atoi函式會對字串進行檢查,如果超過範圍,會報錯,遇到非法字元同樣會停下來,不會報錯;標頭檔案為string

[html]view plain

copy

#include

<

iostream

>

#include<

cstdlib

>

#include<

string

>

using namespace std;  

int stoiint1()  

int stoiint2()  

int stoiint3()  

int main()   

輸出:111111

111terminate called after throwing an instance of 'std::out_of_range'

3、strtol

同樣是將string字串轉換為int型別,但這個函式可以自定義轉換的string字串的型別(第三個引數可定義string字串的型別),strtol超過int範圍,不會報錯,只會輸出最大或者最小值[-2147483648,2147483647],遇到非法字元,則同樣停止,不會報錯;

[html]view plain

copy

#include

<

iostream

>

#include<

cstdlib

>

#include<

string

>

using namespace std;  

int strtolint1()  

int strtolint2()  

int strtolint3()  

int strtolint4()  

int main()  

輸出:63

1112147483647

-2147483648

atoi stoi和strtol的使用

1 atoi 將string字串轉換為int型別,只能轉換為十進位制 atoi函式不會對string字串進行範圍檢查 2147483648,2147483647 超過這個界限,不會報錯,只會進行相應的轉換,遇到非法字元會停止,不會報錯 標頭檔案為cstdlib include include inc...

strtol函式用法

之前想用c寫md5函式用法,中間設計大量進製轉換的內容,於是就查到了strtol這個函式 但是發現之前對其認識上有一些偏頗,所以把它的用法記錄下來 strtol是乙個c語言函式,作用就是將乙個字串轉換為長整型long,其函式原型為 long int strtol const char str,cha...

strtol 函式用法

strtol是乙個c語言函式,作用就是將乙個字串轉換為長整型long,其函式原型為 long int strtol const char str,char endptr,int base 下面我們來看下每個引數的意義 str是要轉換的字元 enptr是指向第乙個不可轉換的字元位置的指標 base的基...