在线文档教程
C++
本土化 | Localizations

std::ctype

STD::Ctype

Defined in header
template< class CharT > class ctype;

类Ctype封装字符分类功能。执行的所有流输入操作std::basic_istream<charT>使用std::ctype<charT>在流中插入区域设置,以标识用于输入标记化的空白字符。流输出操作应用std::ctype<charT>::widen()在输出之前缩小字符参数。

二次

二次

继承图

标准库提供了两个独立的%28区域设置无关%29的专门化:

在标头中定义<locale>

*。

STD::Ctype<char>提供最小“C”区域设置分类的窄字符等效项。此专门化使用表查找进行字符分类。

STD::Ctype<wchar[医]T>提供适用于本机字符集的宽字符分类。

此外,在C++程序中构造的每个locale对象都实现了自己的%28 locale特定于这些专门化的%29版本。

成员类型

Member typeDefinition
char_typeCharT

成员函数

(constructor)constructs a new ctype facet (public member function)
(destructor)destructs a ctype facet (protected member function)
isinvokes do_is (public member function)
scan_isinvokes do_scan_is (public member function)
scan_notinvokes do_scan_not (public member function)
toupperinvokes do_toupper (public member function)
tolowerinvokes do_tolower (public member function)
wideninvokes do_widen (public member function)
narrowinvokes do_narrow (public member function)

成员对象

static std::locale::id idid of the locale (public member object)

受保护成员函数

do_is virtualclassifies a character or a character sequence (virtual protected member function)
do_scan_is virtuallocates the first character in a sequence that conforms to given classification (virtual protected member function)
do_scan_not virtuallocates the first character in a sequence that fails given classification (virtual protected member function)
do_toupper virtualconverts a character or characters to uppercase (virtual protected member function)
do_tolower virtualconverts a character or characters to lowercase (virtual protected member function)
do_widen virtualconverts a character or characters from char to charT (virtual protected member function)
do_narrow virtualconverts a character or characters from charT to char (virtual protected member function)

从STD::Ctype继承而来[医]底座

成员类型

TypeDefinition
maskunspecified bitmask type (enumeration, integer type, or bitset)

成员常数

space staticthe value of mask identifying whitespace character classification (public static member constant)
print staticthe value of mask identifying printable character classification (public static member constant)
cntrl staticthe value of mask identifying control character classification (public static member constant)
upper staticthe value of mask identifying uppercase character classification (public static member constant)
lower staticthe value of mask identifying lowercase character classification (public static member constant)
alpha staticthe value of mask identifying alphabetic character classification (public static member constant)
digit staticthe value of mask identifying digit character classification (public static member constant)
punct staticthe value of mask identifying punctuation character classification (public static member constant)
xdigit staticthe value of mask identifying hexadecimal digit character classification (public static member constant)
blank staticthe value of mask identifying blank character classification (public static member constant)
alnum staticalpha | digit (public static member constant)
graph staticalnum | punct (public static member constant)

下面的示例演示对C类型以外的其他类型的修改<char>来标记CSV文件。

二次

#include <iostream> #include <locale> #include <sstream> struct csv_whitespace : std::ctype<wchar_t> { bool do_is(mask m, char_type c) const { if ((m & space) && c == L' ') { return false; // space will NOT be classified as whitespace } if ((m & space) && c == L',') { return true; // comma will be classified as whitespace } return ctype::do_is(m, c // leave the rest to the parent class } }; int main() { std::wstring in = L"Column 1,Column 2,Column 3\n123,456,789"; std::wstring token; std::wcout << "default locale:\n"; std::wistringstream s1(in while (s1 >> token) { std::wcout << " " << token << '\n'; } std::wcout << "locale with modified ctype:\n"; std::wistringstream s2(in csv_whitespace* my_ws = new csv_whitespace; // note: this allocation is not leaked s2.imbue(std::locale(s2.getloc(), my_ws) while (s2 >> token) { std::wcout << " " << token<< '\n'; } }

二次

产出:

二次

default locale: Column 1,Column 2,Column 3 123,456,789 locale with modified ctype: Column 1 Column 2 Column 3 123 456 789

二次

另见

ctypespecialization of std::ctype for type char (class template specialization)
ctype_basedefines character classification categories (class template)
ctype_bynamecreates a ctype facet for the named locale (class template)

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/locale/ctype