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

📅  最后修改于: 2021-05-26 02:25:22             🧑  作者: Mango

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

语法

wmemset(des, ch, count)

参数:该函数接受以下三个参数。

  • des :它指定到宽字符数组以复制宽字符。
  • ch :指定要复制的宽字符。
  • count :指定要复制的次数。

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

  • 如果计数大于0,则函数返回des
  • 如果计数小于0,则可能会发生分段错误
  • 如果count等于零,则该函数不执行任何操作。

下面的程序说明了上述函数。
程序1

// C++ program to illustrate the
// wmemset() function when count is greater than 0
#include 
#include 
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 
#include 
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 
#include 
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)
想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。