名稱空間
變體
操作

std::isxdigit(std::locale)

來自 cppreference.com
< cpp‎ | locale
 
 
 
 
定義於標頭檔案 <locale>
template< class CharT >
bool isxdigit( CharT ch, const locale& loc );

檢查給定字元是否被給定區域設定的 std::ctype facet 分類為十六進位制數字。

目錄

[編輯] 引數

ch - 字元
loc - locale

[編輯] 返回值

如果字元被分類為十六進位制數字,則返回 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 first Greek small letters will be classified as digits
            : ctype::do_is(m, c); // leave the rest to the parent class
    }
};
 
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 << "Hexadecimal digits in text: ";
    for (const wchar_t c : text)
        if (std::isxdigit(c, loc))
            std::wcout << c << L' ';
    std::wcout << L'\n';
 
    std::wcout << "Not hexadecimal digits in text: ";
    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: η θ ι κ λ μ

[編輯] 參閱

檢查字元是否為十六進位制數字
(函式) [編輯]
檢查寬字元是否為十六進位制字元
(函式) [編輯]