public member function
<locale>

std::ctype::is

單個字元 (1)
            bool is (mask m, char_type c) const;
序列 (2)
const char_type* is (const char_type* low,                     const char_type* high, mask* vec) const;
分類字元
第一個版本(1) 返回字元 c 是否屬於位掩碼 m 所指定的任何類別。

第二個版本(2) 對範圍 [low,high) 中的字元進行分類,用每個字元的位掩碼分類結果順序填充陣列 vec

ctype 的泛型模板中,此函式僅呼叫虛保護成員 do_is,該成員預設執行上述操作。

char 特化 (ctype<char>) 中,此函式使用內部 table 直接返回結果,而不呼叫任何虛成員。

引數

m
成員型別 mask 的位掩碼(繼承自 ctype_base),指定要對照哪個類別檢查字元。如果位掩碼組合了多個類別,則當字元屬於任何一個類別時,函式返回 true
它可以是以下成員位掩碼值的任何按位組合
成員常量描述
space未指定(唯一位)空白字元
print未指定(唯一位)可列印字元
cntrl未指定(唯一位)控制字元
upper未指定(唯一位)大寫字母
lower未指定(唯一位)小寫字母
alpha未指定(唯一位)字母字元
digit未指定(唯一位)十進位制數字
punct未指定(唯一位)標點符號字元
xdigit未指定(唯一位)十六進位制數字
alnumalpha|digit字母數字字元
graphalnum|punct具有圖形表示的字元
成員常量描述
space未指定(唯一位)空白字元
print未指定(唯一位)可列印字元
cntrl未指定(唯一位)控制字元
upper未指定(唯一位)大寫字母
lower未指定(唯一位)小寫字母
alpha未指定(唯一位)字母字元
digit未指定(唯一位)十進位制數字
punct未指定(唯一位)標點符號字元
xdigit未指定(唯一位)十六進位制數字
blank未指定(唯一位)空白字元
alnumalpha|digit字母數字字元
graphalnum|punct具有圖形表示的字元
有關 ASCII 字元如何根據這些類別進行分類的詳細資訊,請參閱 <cctype>
c
要分類的字元。
成員型別 char_type 是字面量的字元型別(定義為 ctype 的模板引數 charT 的別名)。
low, high
字元序列的開始和結束指標。使用的範圍是 [low,high),它包含 lowhigh 之間的所有字元,包括 low 指向的字元,但不包括 high 指向的字元。
請注意,空字元(如果有)也會被分類,並且函式會繼續處理它們之後的內容,直到整個範圍處理完畢。
成員型別 char_type 是字面量的字元型別(定義為 ctype 的模板引數 charT 的別名)。
vec
目標陣列,其儲存空間足以容納至少 (high-low)mask 型別的元素。
此陣列用與每個字元對應的 mask 值組合進行填充。

返回值

第一個版本(1) 如果 c 分類到傳遞的掩碼 m 的任何類別中,則返回 true

第二個版本(2) 返回 high
成員型別 char_type 是字面量的字元型別(定義為 ctype 的模板引數 charT 的別名)。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// ctype::is example
#include <iostream>       // std::cout, std::boolalpha
#include <locale>         // std::locale, std::ctype, std::use_facet

int main ()
{
  std::locale loc;
  const char quote[] = "It is wonderful how much may be done if we are always doing.";
                      // (Attributed to Thomas Jefferson)

  std::cout << '"' << quote << "\"\n";

  // single character:
  std::cout << "The quote begins with an uppercase letter? ";
  std::cout << std::boolalpha;
  std::cout << std::use_facet< std::ctype<char> >(loc).is (std::ctype<char>::upper, quote[0]);
  std::cout << '\n';

  // sequence of characters:
  int cx = 0;
  std::ctype<char>::mask * masks = new std::ctype<char>::mask [60];
  std::use_facet< std::ctype<char> >(loc).is (quote, quote+60, masks);
  for (int i=0; i<60; ++i) if (masks[i] & std::ctype<char>::space) ++cx;
  std::cout << "The quote has " << cx << " whitespaces.\n";
  return 0;
}

輸出

"It is wonderful how much may be done if we are always doing."
The quote begins with an uppercase letter? true
The quote has 12 whitespaces.


資料競爭

該物件被訪問。
(2) 中,會訪問範圍 [low,high) 中的元素,並修改 vec 指向的陣列中的元素。

異常安全

如果丟擲異常,facet 物件不會發生任何變化,儘管目標陣列中的字元可能會受到影響。

另見