std::isdigit(std::locale)
来自cppreference.com
本地化库
本地环境与平面 | |||||||||||||||||||||
本地环境 | |||||||||||||||||||||
平面类别基类 | |||||||||||||||||||||
ctype(字符类别)平面 | |||||||||||||||||||||
numeric(数值)平面 | |||||||||||||||||||||
collate(对照比较)平面 | |||||||||||||||||||||
time(时间)平面 | |||||||||||||||||||||
monetary(货币)平面 | |||||||||||||||||||||
messages(消息)平面 | |||||||||||||||||||||
字符分类与转换 | |||||||||||||||||||||
字符分类 | |||||||||||||||||||||
转换 | |||||||||||||||||||||
|
|||||||||||||||||||||
编码转换平面 | |||||||||||||||||||||
|
|
||||||||||||||||||||
C 本地环境 | |||||||||||||||||||||
在标头
<locale>
定义
|
||
template< class charT >
bool isdigit( charT ch, const locale& loc ); |
||
检查给定字符是否为给定 locale 的 std::ctype 平面分类为数字。
参数
ch | - | 字符 |
loc | - | 本地环境 |
返回值
若字符被分类为数字则返回 true ,否则返回 false 。
可能的实现
template< class charT > bool isdigit( charT ch, const std::locale& loc ) { return std::use_facet<std::ctype<charT>>(loc).is(std::ctype_base::digit, ch); } |
示例
运行此代码
#include <iostream> #include <locale> #include <string> #include <set> struct jdigit_ctype : std::ctype<wchar_t> { std::set<wchar_t> jdigits{L'一',L'二',L'三',L'四',L'五',L'六',L'七',L'八',L'九',L'十'}; bool do_is(mask m, char_type c) const { if ((m & digit) && jdigits.count(c)) return true; // 中文数字将被分类为数字 return ctype::do_is(m, c); // 将剩下的留给亲类 } }; int main() { std::wstring text = L"123一二三123"; std::locale loc(std::locale(""), new jdigit_ctype); std::locale::global(std::locale("")); std::wcout.imbue(std::locale()); for(wchar_t c : text) if(std::isdigit(c, loc)) std::wcout << c << " is a digit\n"; else std::wcout << c << " is NOT a digit\n"; }
输出:
1 is a digit 2 is a digit 3 is a digit 一 is a digit 二 is a digit 三 is a digit 1 is NOT a digit 2 is NOT a digit 3 is NOT a digit
参阅
检查字符是否为数字 (函数) |
|
检查宽字符是否为数字 (函数) |