gpt4 book ai didi

c++ - isdigit() 和 isnumber() 有什么区别?

转载 作者:太空狗 更新时间:2023-10-29 23:32:28 24 4
gpt4 key购买 nike

我的 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!!!

最佳答案

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

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


此外,isnumber() 在 Linux 上没有声明:我在 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)) {
^

关于c++ - isdigit() 和 isnumber() 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39204080/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com