Namespaces
Variants

std:: isxdigit (std::locale)

From cppreference.net
ヘッダーで定義 <locale>
template < class CharT >
bool isxdigit ( CharT ch, const locale & loc ) ;

指定されたロケールの std::ctype ファセットによって、指定された文字が16進数字として分類されるかどうかをチェックします。

目次

パラメータ

ch - 文字
loc - ロケール

戻り値

文字が16進数の数字として分類される場合は 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 << "テキスト内の16進数字: ";
    for (const wchar_t c : text)
        if (std::isxdigit(c, loc))
            std::wcout << c << L' ';
    std::wcout << L'\n';
    std::wcout << "テキスト内の非16進数字: ";
    for (const wchar_t c : text)
        if (not std::isxdigit(c, loc))
            std::wcout << c << L' ';
    std::wcout << L'\n';
}

出力:

テキスト内の16進数字: 0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F α β γ δ ε ζ
テキスト内の非16進数字: η θ ι κ λ μ

関連項目

文字が16進数の文字であるかどうかをチェックする
(関数)
ワイド文字が16進数の文字であるかどうかをチェックする
(関数)