名稱空間
變體
操作

std::ctype

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

ctype 封裝了字元分類特性。所有透過 std::basic_istream<CharT> 執行的流輸入操作,都使用流中浸潤的 locale 的 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 構面
(公開成員函式)
析構一個 ctype 構面
(受保護成員函式)
呼叫 do_is
(public 成員函式) [編輯]
呼叫 do_scan_is
(public 成員函式) [編輯]
呼叫 do_scan_not
(public 成員函式) [編輯]
呼叫 do_toupper
(public 成員函式) [編輯]
呼叫 do_tolower
(public 成員函式) [編輯]
呼叫 do_widen
(public 成員函式) [編輯]
呼叫 do_narrow
(public 成員函式) [編輯]

[編輯] 受保護的成員函式

[虛擬函式]
分類一個字元或一個字元序列
(虛保護成員函式) [編輯]
[虛擬函式]
在一個序列中定位第一個符合給定分類的字元
(虛保護成員函式) [編輯]
[虛擬函式]
在一個序列中定位第一個不符合給定分類的字元
(虛保護成員函式) [編輯]
[虛擬函式]
將一個或多個字元轉換為大寫
(虛保護成員函式) [編輯]
[虛擬函式]
將一個或多個字元轉換為小寫
(虛保護成員函式) [編輯]
[虛擬函式]
將一個或多個字元從 char 轉換為 CharT
(虛保護成員函式) [編輯]
[虛擬函式]
將一個或多個字元從 CharT 轉換為 char
(虛保護成員函式) [編輯]

繼承自 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 (而非 ctype<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 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';
}

輸出

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

[編輯] 參閱

型別 charstd::ctype 特化
(類模板特化) [編輯]
定義字元分類類別
(類) [編輯]
表示具名 locale 的系統提供的 std::ctype
(類模板) [編輯]