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 type | Definition |
---|---|
char_type | CharT |
成员函数
(constructor) | constructs a new ctype facet (public member function) |
---|---|
(destructor) | destructs a ctype facet (protected member function) |
is | invokes do_is (public member function) |
scan_is | invokes do_scan_is (public member function) |
scan_not | invokes do_scan_not (public member function) |
toupper | invokes do_toupper (public member function) |
tolower | invokes do_tolower (public member function) |
widen | invokes do_widen (public member function) |
narrow | invokes do_narrow (public member function) |
成员对象
static std::locale::id id | id of the locale (public member object) |
---|
受保护成员函数
do_is virtual | classifies a character or a character sequence (virtual protected member function) |
---|---|
do_scan_is virtual | locates the first character in a sequence that conforms to given classification (virtual protected member function) |
do_scan_not virtual | locates the first character in a sequence that fails given classification (virtual protected member function) |
do_toupper virtual | converts a character or characters to uppercase (virtual protected member function) |
do_tolower virtual | converts a character or characters to lowercase (virtual protected member function) |
do_widen virtual | converts a character or characters from char to charT (virtual protected member function) |
do_narrow virtual | converts a character or characters from charT to char (virtual protected member function) |
从STD::Ctype继承而来[医]底座
成员类型
Type | Definition |
---|---|
mask | unspecified bitmask type (enumeration, integer type, or bitset) |
成员常数
space static | the value of mask identifying whitespace character classification (public static member constant) |
---|---|
print static | the value of mask identifying printable character classification (public static member constant) |
cntrl static | the value of mask identifying control character classification (public static member constant) |
upper static | the value of mask identifying uppercase character classification (public static member constant) |
lower static | the value of mask identifying lowercase character classification (public static member constant) |
alpha static | the value of mask identifying alphabetic character classification (public static member constant) |
digit static | the value of mask identifying digit character classification (public static member constant) |
punct static | the value of mask identifying punctuation character classification (public static member constant) |
xdigit static | the value of mask identifying hexadecimal digit character classification (public static member constant) |
blank static | the value of mask identifying blank character classification (public static member constant) |
alnum static | alpha | digit (public static member constant) |
graph static | alnum | 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
二次
另见
ctype | specialization of std::ctype for type char (class template specialization) |
---|---|
ctype_base | defines character classification categories (class template) |
ctype_byname | creates a ctype facet for the named locale (class template) |
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。