C ++ STL中的iswdigit()函数
在C++STL中,iswdigit()function是一个内置函数,用于检查给定的宽字符是十进制数字字符还是其他字符。此函数存在于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确实是一个十进制数字,则该值不同于零(即true),否则返回零(即false)。
以下程序中使用的方法如下
在一个变量中输入字符串,比如说string类型的str[]
调用该函数iswdigit()以检查给定的宽字符是否为十进制数字
打印结果
示例1
#include <cwctype>
#include <iostream>
using namespace std;
int main(){
wchar_t c_1 = '2';
wchar_t c_2 = '*';
//功能检查字符
//是不是数字
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