isdigit()和isnumber()有什么区别?

时间:2016-08-29 10:32:30

标签: c++ string ctype

我的ctype.h中有函数isnumber()。我没有在http://www.cplusplus.com/reference/cctype/的书中找到这个功能的参考资料。 我打算通过一个简单的例子检测并显示isdigit()和isnumber()之间的区别,但是没有(此外,U + 0C6A,U + 0ED2和½都不能被函数检测到)。 有什么区别吗?我怎么解决这个问题?

int main(int, char *[])
{
    string text("Hello½123, Poly౪ ໒girl0.5!!!");
    decltype(text.size()) punct = 0, alpha = 0, number = 0, space = 0, digit = 0, graf = 0;
    for(auto i : text) {
        if(ispunct(i)){
            cout << i << "<punct  ";
            ++punct;
        }
        if(isalpha(i)) {
            cout << i << "<alpha  ";
            ++alpha;
        }
        if(isnumber(i)) {
            cout << i << "<number  ";
            ++number;
        }
        if(isdigit(i)) {
            cout << i << "<digit  ";
            ++digit;
        }
        if(isgraph(i)) {
            cout << i << "<graph  ";
            ++graf;
        }
        if(isspace(i))
            ++space;
    }
    cout << endl << "There is " << endl;
    cout << punct <<  " puncts," << endl;
    cout << alpha << " alphas," << endl;
    cout << number << " numbers," << endl;
    cout << digit << " digits," << endl;
    cout << graf << " graphs," << endl;
    cout << space << " spaces" << endl;
    cout << "in " << text << endl;

    return 0;
}

结果的一部分:

...

5 numbers,
5 digits,
23 graphs,
2 spaces
in Hello½123, Poly౪ ໒girl0.5!!!

3 个答案:

答案 0 :(得分:5)

isnumber()可能是特定于Apple的C ++方法(我手头没有Mac可供检查)。 您可以在Apple dev guide

中看到它
  

isnumber()函数的行为与isdigit()类似,但可能会识别其他字符,具体取决于当前的区域设置。

此外,Linux上未声明isnumber(): 我在Linux 4.7.2上使用g ++ 6.1.1并得到错误:

g++ a.cpp
a.cpp: In function 'int main(int, char**)':
a.cpp:20:17: error: 'isnumber' was not declared in this scope
   if (isnumber(i)) {
                 ^

我也使用clang3.8.1来测试:

clang++ a.cpp --std=c++11
a.cpp:20:7: error: use of undeclared identifier 'isnumber'
                if (isnumber(i)) {
                    ^

答案 1 :(得分:3)

isdigit()仅适用于0-9。

isnumber()允许其他数字值,例如分数。一些“数字”但不是数字的字符包括0x00b2和0x00b3,它们是上标2和3('²'和'³')以及分数的字形,如'¼','½'和'¾'。

答案 2 :(得分:1)

Apple添加了isnumber()功能,因此您需要使用Apple文档。

以下是the ctype.h manpage for iOS

的引用
STANDARDS
     These functions, except for digittoint(), isascii(), ishexnumber(),
     isideogram(), isnumber(), isphonogram(), isrune(), isspecial() and
     toascii(), conform to ISO/IEC 9899:1990 (``ISO C90'').

同一页面实际上并未链接到isnumber()的联机帮助页,但我从标准函数的联机帮助页链接中推断了其URL,并找到了this

The isnumber() function behaves similarly to isdigit(), but may recognize
additional characters, depending on the current locale setting.

Google isnumber ctype的结果#4 ...