名稱空間
變體
操作

std::isdigit(std::locale)

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

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

目錄

[編輯] 引數

ch - 字元
loc - locale

[編輯] 返回值

如果字元被分類為數字,則返回 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 // Japanese digits will be classified as digits
            : ctype::do_is(m, c); // leave the rest to the parent class
    }
};
 
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

[編輯] 參閱

檢查字元是否為數字
(函式) [編輯]
檢查寬字元是否為數字
(函式) [編輯]