繁星淼淼
在cctype头文件中有相当数量的,你可以在字符串中的每个字符使用字符分类功能。对于数字检查,应为isdigit。以下程序显示了如何检查C或C ++字符串的每个字符(就检查实际字符而言,该过程几乎是相同的,唯一真正的区别是如何获得长度):#include <iostream>#include <cstring>#include <cctype>int main (void) { const char *xyzzy = "42x"; std::cout << xyzzy << '\n'; for (int i = 0; i < std::strlen (xyzzy); i++) { if (! std::isdigit (xyzzy[i])) { std::cout << xyzzy[i] << " is not numeric.\n"; } } std::string plugh ("3141y59"); std::cout << plugh << '\n'; for (int i = 0; i < plugh.length(); i++) { if (! std::isdigit (plugh[i])) { std::cout << plugh[i] << " is not numeric.\n"; } } return 0;}