【C、C++】ctype.h、cctype中的isnumber()函式和isdigit()函式的區別
isnumber()函式和isdigit()函式並無區別,檢視函式定義發現也是相同的,可能有微小的區別, iPhoneOS/man3/ctype.3.html" rel="nofollow,noindex" target="_blank">Mac OS X 手冊頁 上ctype函式中對isnumber()和isdigit()函式的區別是這樣解釋的:
The isnumber() function behaves similarly to isdigit(), but may recognize additional characters, depending on the current locale setting.
也就是說:isnumber()函式與isdigit()相似,但isnumber()可能會識別其他字元,具體取決於當前的區域設定~
在XCode或者CLion(Mac版)的ctype.h(或者cctype)中可能會見到isnumber()函式,然而查閱C++官方文件卻找不到,這是因為isnumber()並不是C/C++的官方庫函式,而是Apple特地為Mac OS的C++新增的它認為好用的一些函式(這些函式有很多,不止isnumber)。可以檢視 Mac OS X Man Pages中ctype函式頁 找到isnumber()函式~手冊中也提到isnumber()並不在C90標準中:
These functions, except for digittoint(), isascii(), ishexnumber(), isideogram(), isnumber(), isphonogram(), isrune(), isspecial() and toascii(), conform to ISO/IEC 9899:1990 ( ISO C90”).
可以看到如果使用isnumber()函式在Linux環境下g++是無法編譯通過的:
g++ a.cpp a.cpp: In function 'int main()': a.cpp:20:17: error: 'isnumber' was not declared in this scope if (isnumber(c)) { ^
在Clang下也是無法編譯通過的:
clang++ a.cpp --std=c++11 a.cpp:20:7: error: use of undeclared identifier 'isnumber' if (isnumber(c)) { ^
所以isnumber()並不是標準的C/C++庫函式,如果是刷演算法的時候還是不要使用哦~
❤❤點選這裡 -> 訂閱PAT、藍橋杯、GPLT天梯賽、LeetCode題解離線版❤❤