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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP