Namespaces
Variants

std:: isdigit (std::locale)

From cppreference.net
헤더 파일에 정의됨 <locale>
template < class CharT >
bool isdigit ( CharT ch, const locale & loc ) ;

주어진 문자가 주어진 로캘의 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 <unordered_set>
struct jdigit_ctype : std::ctype<wchar_t>
{
    std::unordered_set<wchar_t> jdigits{
        L'一', L'二', L'三', L'四', L'五', L'六', L'七', L'八', L'九', L'十'
    };
    bool do_is(mask m, char_type c) const override
    {
        return (m & digit) && jdigits.contains(c)
            ? true // 일본어 숫자는 숫자로 분류됨
            : 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("en_US.utf8"));
    std::wcout.imbue(std::locale());
    for (const 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

참고 항목

문자가 숫자인지 확인합니다
(함수)
와이드 문자가 숫자인지 확인합니다
(함수)