命名空间
变体
操作

std::ctype

来自 cppreference.cn
< cpp‎ | locale
 
 
 
 
 
定义于头文件 <locale>
template< class CharT >
class ctype;

ctype 类封装了字符分类功能。所有通过 std::basic_istream<CharT> 执行的流输入操作都使用流中注入的本地环境的 std::ctype<CharT> 来识别用于输入标记化的空白字符。流输出操作在输出前将 std::ctype<CharT>::widen() 应用于窄字符参数。

cpp/locale/ctype basecpp/locale/locale/facetstd-ctype-inheritance.svg

继承关系图

内容

[编辑] 特化

标准库保证提供以下特化(它们需要由任何本地环境对象实现

定义于头文件 <locale>
std::ctype<char> 提供最小 “C” 本地环境分类的窄字符等效项。此特化使用表查找进行字符分类
std::ctype<wchar_t> 提供适合于本机字符集的宽字符分类

[编辑] 嵌套类型

类型 定义
char_type CharT

[编辑] 数据成员

成员 描述
std::locale::id id [static] facet 的标识符

[编辑] 成员函数

构造新的 ctype facet
(公共成员函数)
析构 ctype facet
(受保护的成员函数)
调用 do_is
(公共成员函数) [编辑]
调用 do_scan_is
(公共成员函数) [编辑]
调用 do_scan_not
(公共成员函数) [编辑]
toupper
调用 do_toupper
(公共成员函数) [编辑]
tolower
(公共成员函数) [编辑]
widen
(公共成员函数) [编辑]
narrow

调用 do_narrow

[编辑] 受保护的成员函数
do_is
[virtual]
(虚拟受保护的成员函数) [编辑]
do_scan_is
(虚拟受保护的成员函数) [编辑]
do_scan_not
(虚拟受保护的成员函数) [编辑]
do_toupper
[编辑] 受保护的成员函数
(虚拟受保护的成员函数) [编辑]
do_tolower
[编辑] 受保护的成员函数
(虚拟受保护的成员函数) [编辑]
do_widen
(虚拟受保护的成员函数) [编辑]
do_narrow

将字符或字符从 CharT 转换为 char

(虚拟受保护的成员函数) [编辑]

类型 定义
继承自 std::ctype_base 嵌套类型

mask

未指定的 BitmaskType 类型(枚举、整数类型或位集)
成员常量
space
[static]
标识空白字符分类的 mask
成员常量
(公共静态成员常量)
[static]
print
成员常量
标识可打印字符分类的 mask
[static]
cntrl
成员常量
标识控制字符分类的 mask
[static]
upper
成员常量
标识大写字符分类的 mask
[static]
lower
成员常量
标识小写字符分类的 mask
[static]
alpha
成员常量
标识字母字符分类的 mask
[static]
digit
成员常量
标识数字字符分类的 mask
[static]
punct
成员常量
标识标点字符分类的 mask
[static]
xdigit
标识十六进制数字字符分类的 mask
blank
[static]
[static] (C++11)
成员常量
标识空白字符分类的 mask
[static]
alnum
成员常量
alpha | digit
[static]

graph

alnum | punct

#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 base 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;
    s2.imbue(std::locale(s2.getloc(), my_ws));
    while (s2 >> token)
        std::wcout << "  " << token << '\n';
}

以下示例演示了修改 ctypectype<char> 除外)以标记化 CSV 文件

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<char>
std::ctype 类型 char 的特化
(类模板特化) [编辑]
定义字符分类类别
(类) [编辑]
表示系统提供的用于命名本地环境的 std::ctype