名稱空間
變體
操作

std::ctype<char>

來自 cppreference.com
< cpp‎ | locale
 
 
 
 
定義於標頭檔案 <locale>
template<>
class ctype<char>;

這個 std::ctype 的特化為 char 型別封裝了字元分類特性。與使用虛擬函式的一般用途的 std::ctype 不同,此特化使用表格查詢來分類字元(通常更快)。

基類 std::ctype<char> 實現了與最小“C”區域設定等效的字元分類。如果使用非預設分類表引數構造,或者構造為 std::ctype_byname<char> 或使用者定義的派生 facet,則分類規則可以擴充套件或修改。所有 std::istream 格式化輸入函式都必須在輸入解析期間使用 std::ctype<char> 進行字元分類。

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

繼承圖

目錄

[編輯] 巢狀型別

型別 定義
char_type char

[編輯] 資料成員

成員 描述
std::locale::id id [static] facet 的識別符號
const std::size_t table_size [static] 分類表的大小,至少 256

[編輯] 成員函式

構造一個新的 ctype<char> facet
(public member function) [編輯]
銷燬一個 ctype<char> facet
(protected member function) [編輯]
獲取字元分類表
(public member function) [編輯]
獲取“C”區域設定字元分類表
(public static member function) [編輯]
使用分類表對字元或字元序列進行分類
(public member function) [編輯]
使用分類表在序列中定位第一個符合給定分類的字元
(public member function) [編輯]
使用分類表在序列中定位第一個不符合給定分類的字元
(public member function) [編輯]
呼叫 do_toupper
(std::ctype<CharT> 的公共成員函式) [編輯]
呼叫 do_tolower
(std::ctype<CharT> 的公共成員函式) [編輯]
呼叫 do_widen
(std::ctype<CharT> 的公共成員函式) [編輯]
呼叫 do_narrow
(std::ctype<CharT> 的公共成員函式) [編輯]

[編輯] 保護成員函式

[virtual]
將一個或多個字元轉換為大寫
(std::ctype<CharT> 的虛保護成員函式) [編輯]
[virtual]
將一個或多個字元轉換為小寫
(std::ctype<CharT> 的虛保護成員函式) [編輯]
[virtual]
將一個或多個字元從 char 轉換為 CharT
(std::ctype<CharT> 的虛保護成員函式) [編輯]
[virtual]
將一個或多個字元從 CharT 轉換為 char
(std::ctype<CharT> 的虛保護成員函式) [編輯]

繼承自 std::ctype_base

巢狀型別

型別 定義
mask 未指定的 BitmaskType 型別(列舉、整數型別或位集)

成員常量

space
[靜態]
標識空白字元分類的 mask
(public static 成員常量)
print
[靜態]
標識可列印字元分類的 mask
(public static 成員常量)
cntrl
[靜態]
標識控制字元分類的 mask
(public static 成員常量)
upper
[靜態]
標識大寫字元分類的 mask
(public static 成員常量)
lower
[靜態]
標識小寫字元分類的 mask
(public static 成員常量)
alpha
[靜態]
標識字母字元分類的 mask
(public static 成員常量)
digit
[靜態]
標識數字字元分類的 mask
(public static 成員常量)
punct
[靜態]
標識標點字元分類的 mask
(public static 成員常量)
xdigit
[靜態]
標識十六進位制數字字元分類的 mask
(public static 成員常量)
blank
[靜態] (C++11)
標識空白字元分類的 mask
(public static 成員常量)
alnum
[靜態]
alpha | digit
(public static 成員常量)
graph
[靜態]
alnum | punct
(public static 成員常量)

[編輯] 示例

以下示例演示了修改 ctype<char> 以分詞逗號分隔的值

#include <cstddef>
#include <iostream>
#include <locale>
#include <sstream>
#include <vector>
 
// This ctype facet classifies commas and endlines as whitespace
struct csv_whitespace : std::ctype<char>
{
    static const mask* make_table()
    {
        // make a copy of the "C" locale table
        static std::vector<mask> v(classic_table(), classic_table() + table_size);
        v[','] |=  space; // comma will be classified as whitespace
        v[' '] &= ~space; // space will not be classified as whitespace
        return &v[0];
    }
 
    csv_whitespace(std::size_t refs = 0) : ctype(make_table(), false, refs) {}
};
 
int main()
{
    std::string in = "Column 1,Column 2,Column 3\n123,456,789";
    std::string token;
 
    std::cout << "Default locale:\n";
    std::istringstream s1(in);
    while (s1 >> token)
        std::cout << "  " << token << '\n';
 
    std::cout << "Locale with modified ctype:\n";
    std::istringstream s2(in);
    s2.imbue(std::locale(s2.getloc(), new csv_whitespace));
    while (s2 >> token)
        std::cout << "  " << 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

[編輯] 缺陷報告

下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。

缺陷報告 應用於 釋出時的行為 正確的行為
LWG 695 C++98 table()classic_table() 是保護成員函式 使它們成為公共的

[編輯] 參閱

定義字元分類表
(類模板) [編輯]
定義字元分類類別
(類) [編輯]
表示命名區域設定的系統提供的 std::ctype
(類模板) [編輯]