正确做法是将 char 显式转为 unsigned char 再传 std::isdigit,因后者要求参数在 0–255 或 EOF;否则有符号 char 负值会导致未定义行为,且空字符串时 all_of 返回 true 需按需额外检查。
直接用 std::all_of 配合 std::isdigit 判断字符串是否全数字,是常见且简洁的做法,但必须注意 std::isdigit 的参数类型和 locale 依赖问题——否则在非 ASCII 环境或含负号/空格的字符串上会出错甚至触发未定义行为。
std::isdigit 原型是 int isdigit(int ch),它要求参数为 unsigned char 范围内的值(0–255)或 EOF。如果 char 在当前平台是有符号类型(如大多数 x86_64 Linux/macOS),那么像 'ÿ'(值为 -1)这样的字符会被提升为负的 int,传给 isdigit 就违反了函数要求,导致未定义行为。
实操建议:
char 显式转换为 unsigned char 再传给 std::isdigit
std::isdigit(std::locale()) 重载,但需传入 charT 和 locale,更重,一般没必要核心是:遍历字符串每个字符,对每个 c 检查 std::isdigit(static_cast 是否为真。
std::string s = "12345";
bool is_all_digits = std::all_of(s.begin(), s.end(), [](char c) {
return std::isdigit(static_cast(c));
});
常见错误写法(危险):
std::all_of(..., [](char c) { return std::isdigit(c); }) —— 缺少 static_cast
std::all_of(..., std::isdigit) —— 类型不匹配,编译失败(std::isdigit 是函数指针,签名不符)true —— 这是 std::all_of 的语义(空范围视为全部满足),若业务要求“至少一位数字”,需额外检查 s.empty()
如果只处理 ASCII 数字、且想避免 locale 和类型陷阱,手动循环可能更直白可控:
bool is_all_digits(const std::string& s) {
if (s.empty()) return false; // 或按需调整
for (char c : s) {
if (c < '0' || c > '9') return false;
}
return true;
}
优势:
缺点:

std::string 本身也不含 Unicode 语义,除非你用 UTF-8 编码并自行解析码点std::isdigit 默认使用 "C" locale,此时只认 `'0'`–`'9'`;但若程序中调用了 std::setlocale(LC_ALL, "") 或构造了非 C locale 的 std::locale 对象,并传给带 locale 版本的 std::isdigit,就可能识别其他数字字符(如阿拉伯-印度数字)。不过标准库中无参数版 std::isdigit **永远不读取当前 locale**,它始终是 C locale 行为。
所以实际影响很小,但要注意:
std::isdigit 会随 setlocale 改变行为 —— 它不会std::use_facet<:ctype>>(loc).is(std::ctype_base::digit, c) 或 C++20 的 std::isdigit(c, loc)
真正容易被忽略的是:std::all_of 对空字符串返回 true,而多数业务场景下“空”不等于“全数字”。要不要加 !s.empty() 判断,得看你的输入来源和协议约定——这比 static_cast 还容易漏掉。