C/C++中的wmemset()及其示例

这个 wmemset() 函数是C/C++中的一个内置函数,它将单个宽字符在指定的时间内复制到宽字符数组中。它是在 cwchar 头文件在C++中。

null

语法 :

wmemset(des, ch, count)

参数 :该函数接受三个参数,如下所述。

  • 德斯 :它指定要复制宽字符的宽字符数组。
  • 中国 :指定要复制的宽字符。
  • 计数 :指定要复制的次数。

返回值 :函数返回三个值,如下所示:

  • 如果 计数 大于0,则函数返回 德斯 .
  • 如果 计数 小于0, 分段故障 可能发生。
  • 如果 计数 等于零,函数什么都不做。

下面的程序说明了上述功能。 方案1 :

// C++ program to illustrate the
// wmemset() function when count is greater than 0
#include <cwchar>
#include <iostream>
using namespace std;
int main()
{
wchar_t ch = L 'G' ;
wchar_t des[20];
int count = 10;
wmemset(des, ch, count);
wcout << L "After copying " << ch << L " 10 times" << endl;
for ( int i = 0; i < count; i++)
putwchar(des[i]);
return 0;
}


输出:

After copying G 10 times
GGGGGGGGGG

方案2 :

// C++ program to illustrate the
// wmemset() function when count is 0
#include <cwchar>
#include <iostream>
using namespace std;
int main()
{
wchar_t ch = L 'r' ;
wchar_t des[20];
int count = 0;
wmemset(des, ch, count);
wcout << L "After copying " << ch << L " 0 times" << endl;
for ( int i = 0; i < count; i++)
putwchar(des[i]);
return 0;
}


输出:

After copying r 0 times

方案3 :

// C++ program to illustrate the
// wmemset() function when
// count is less than 0
// returns a segmentation fault
#include <cwchar>
#include <iostream>
using namespace std;
int main()
{
wchar_t ch = L 'q' ;
wchar_t des[20];
int count = -4;
wmemset(des, ch, count);
wcout << L "After copying " << ch << L " -4 times" << endl;
for ( int i = 0; i < count; i++)
putwchar(des[i]);
return 0;
}


输出:

Runtime Errors:
Segmentation Fault (SIGSEGV)
© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享