C++ STL 中的 iswspace() 函数
c++server side programmingprogramming
本文将讨论 C++ 中的 iswspace() 函数,包括其语法、工作原理及其返回值。
iswspace() 函数是 C++ 中的一个内置函数,定义在头文件中。该函数检查传递的宽字符是否为空格字符。该函数相当于 isspace() 的宽字符版本,这意味着它的工作方式与 isspace() 相同,不同之处在于它支持宽字符。该函数检查传递的参数是否为空格("‘ ‘"),如果为空格,则返回非零整数值 (true),否则返回零 (false)。
语法
int iswspace(wint_t ch);
该函数仅接受一个参数,即要检查的宽字符。参数以 wint_t 或 WEOF 类型转换。
wint_t 存储整数类型的数据。
返回值
该函数返回一个整数值,可以为 0(如果为 false)或任何非零值(如果为 true)。
示例
#include <iostream> #include <cwctype> using namespace std; int main() { wint_t a = '.'; wint_t b = ' '; wint_t c = '1'; iswspace(a)?cout<<"\nIts white space character":cout<<"\nNot white space character"; iswspace(b)?cout<<"\nIts white space character":cout<<"\nNot white space character"; iswspace(c)?cout<<"\nIts white space character":cout<<"\nNot white space character"; }
输出
如果我们运行上述代码,它将生成以下输出 −
Not white space character Its white space character Not white space character
示例
#include <iostream> #include <cwctype> using namespace std; int main () { int i, count; wchar_t s[] = L"I am visiting tutorials point"; count = i = 0; while (s[i]) { if(iswspace(s[i])) count++; i++; } cout<<"There are "<<count <<" white space characters.\n"; return 0; }
输出
如果我们运行上述代码,它将生成以下输出 −
There are 4 white space characters.