Namespaces
Variants

std:: isxdigit (std::locale)

From cppreference.net
定义于头文件 <locale>
template < class CharT >
bool isxdigit ( CharT ch, const locale & loc ) ;

检查给定字符是否被指定区域的 std::ctype 刻面归类为十六进制数字。

目录

参数

ch - 字符
loc - 区域设置

返回值

当字符被分类为十六进制数字时返回 true ,否则返回 false

可能的实现

template<class CharT>
bool isxdigit(CharT ch, const std::locale& loc)
{
    return std::use_facet<std::ctype<CharT>>(loc).is(std::ctype_base::xdigit, ch);
}

示例

#include <iostream>
#include <locale>
#include <string>
#include <unordered_set>
struct gxdigit_ctype : std::ctype<wchar_t>
{
    std::unordered_set<wchar_t> greek_digits{L'α', L'β', L'γ', L'δ', L'ε', L'ζ'};
    bool do_is(mask m, char_type c) const override
    {
        return (m & xdigit) && greek_digits.contains(c)
            ? true // 前6个希腊小写字母将被分类为数字
            : ctype::do_is(m, c); // 其余字符交由父类处理
    }
};
int main()
{
    std::wstring text = L"0123456789abcdefABCDEFαβγδεζηθικλμ";
    std::locale loc(std::locale(""), new gxdigit_ctype);
    std::locale::global(std::locale("en_US.utf8"));
    std::wcout.imbue(std::locale());
    std::wcout << "文本中的十六进制数字: ";
    for (const wchar_t c : text)
        if (std::isxdigit(c, loc))
            std::wcout << c << L' ';
    std::wcout << L'\n';
    std::wcout << "文本中的非十六进制数字: ";
    for (const wchar_t c : text)
        if (not std::isxdigit(c, loc))
            std::wcout << c << L' ';
    std::wcout << L'\n';
}

输出:

Hexadecimal digits in text: 0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F α β γ δ ε ζ
Not hexadecimal digits in text: η θ ι κ λ μ

参见

检查字符是否为十六进制字符
(函数)
检查宽字符是否为十六进制字符
(函数)