名前空間
変種
操作

std::isdigit(std::locale)

提供: cppreference.com
< cpp‎ | locale
 
 
 
ヘッダ <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 <set>
 
struct jdigit_ctype : std::ctype<wchar_t>
{
    std::set<wchar_t> jdigits{L'一',L'二',L'三',L'四',L'五',L'六',L'七',L'八',L'九',L'十'};
    bool do_is(mask m, char_type c) const {
        if ((m & digit) && jdigits.count(c))
            return true; // Japanese digits will be classified as digits
        return 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(""));
    std::wcout.imbue(std::locale());
 
    for(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

[編集] 関連項目

文字が数字かどうか調べます
(関数) [edit]
ワイド文字が数字かどうか調べます
(関数)