C++ STL 中的 iswpunct() 函数

c++server side programmingprogramming

本文将讨论 C++ 中的 iswpunct() 函数,包括其语法、工作原理及其返回值。

iswpunct() 函数是 C++ 中的一个内置函数,定义在 <cwctype> 头文件中。该函数用于检查传递的宽字符是否为标点符号。该函数相当于 ispunct() 的宽字符版本,这意味着它与 ispunct() 的工作原理相同,不同之处在于它支持宽字符。因此,该函数检查传递的参数是否为标点符号,如果为真,则返回任何非零整数值 (true),否则返回零 (false)。

标点符号如下

! @ # $ % ^ & * ( ) “ ‘ , . / ; [ { } ] : ?

语法

int iswpunct(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 = 'a';
   wint_t c = '1';
   iswpunct(a)?cout<<"\nIts Punctuation character":cout<<"\nNot Punctuation character";
   iswpunct(b)?cout<<"\nIts Punctuation character":cout<<"\nNot Punctuation character";
   iswpunct(c)?cout<<"\nIts Punctuation character":cout<<"\nNot Punctuation character";
}

输出

如果我们运行上述代码,它将生成以下输出 −

Its Punctuation character
Not Punctuation character
Not Punctuation character

示例

#include <iostream>
#include <cwctype>
using namespace std;
int main () {
   int i, count;
   wchar_t s[] = L"@tutorials, point!!";
   count = i = 0;
   while (s[i]) {
      if(iswpunct(s[i]))
      count++;
      i++;
   }
   cout<<"There are "<<count <<" punctuation characters.\n";
   return 0;
}

输出

如果我们运行上述代码,它将生成以下输出 −

There are 4 punctuation characters.

相关文章