C++ STL 中的 iswdigit() 函式


在 C++ STL 中,iswdigit() 函式是一個內建函式,用於檢查給定的寬字元是十進位制數字字元還是其他一些字元。此函式存在於 C/C++ 中的 cwctype 標頭檔案中。

什麼是十進位制數字字元?

十進位制數字字元是從 0 開始的數字值,即 0、1、2、3、4、5、6、7、8、9。

iswcntrl() 函式的語法如下

int iswdigit() (wint_t c)

引數 − c 是要檢查的寬字元,轉換為 wint_t 或 WEOF,其中 wint_t 是一個整數型別。

返回值 − 若 c 確實為十進位制數字,則返回一個非零值(即為真),否則返回零值(即為假)。

以下程式中使用的方法如下

  • 在一個字串型別變數(如 str[])中輸入字串

  • 呼叫函式 iswdigit(),以檢查給定的寬字元是否是十進位制數字

  • 列印結果

示例 1

 線上演示

#include <cwctype>
#include <iostream>
using namespace std;
int main(){
   wchar_t c_1 = '2';
   wchar_t c_2 = '*';
   // Function to check if the character
   // is a digit or not
   if (iswdigit(c_1))
      wcout << c_1 << " is a character ";
   else
      wcout << c_1 << " is a digit ";
      wcout << endl;
   if (iswdigit(c_2))
      wcout << c_2 << " is a character ";
   else
      wcout << c_2 << " is a digit ";
   return 0;
}

輸出

如果執行以上程式碼,將生成以下輸出 −

2 is a digit
* is a character

示例 2

 線上演示

#include <stdio.h>
#include <wchar.h>
#include <wctype.h>
int main (){
   wchar_t str[] = L"1776ad";
   long int year;
   if (iswdigit(str[0])) {
      year = wcstol (str,NULL,10);
      wprintf (L"The year that followed %ld was %ld.\n",year,year+1);
   }
   return 0;
}

輸出

如果執行以上程式碼,將生成以下輸出 −

The year 1777 followed 1776

更新於: 30-1 月-2020

84 次瀏覽

啟動您的職業生涯

完成課程以獲得認證

開始
廣告
© . All rights reserved.