当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C++ wmemcpy()用法及代码示例


wmemcpy()函数在头文件cwchar.h中指定,并将指定数量的字符从一个字符串复制到另一个字符串。此函数不会检查第一个称为source的字符串中是否有任何终止的全角宽字符,它始终将正好n个字符复制到第二个称为destination的字符串中。

用法:

wchar_t* wmemcpy( wchar_t* destination, const wchar_t* source, size_t n )

参数:该函数接受三个强制性参数,如下所述:


  • destination:指定要将字符复制的指针。
  • source:指定存在数据的指针。
  • n:指定要复制的字符数。

返回值:该函数返回目标字符串。

以下示例程序旨在说明上述函数:
程序1:

// C++ program to illustarte 
// wmemcpy() function 
  
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
    // initialize the destination size 
    wchar_t destination[20]; 
  
    // initialize the source string 
    wchar_t source[] = L"geeks are for geeks"; 
  
    // till number of characters 
    int n = 13; 
  
    // function to copy from source to 
    // destination 
    wmemcpy(destination, source, n); 
  
    wcout << L"Initial string -> " << source << "\n"; 
  
    // print the copied string 
    wcout << L"Final string -> "; 
    for (int i = 0; i < n; i++) 
        putwchar(destination[i]); 
  
    return 0; 
}
输出:
Initial string -> geeks are for geeks
Final string -> geeks are for

程序2:

// C++ program to illustarte 
// wmemcpy() function 
// when 'n' is equal to the number of 
// characters in the source string 
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
    // initialize the destination size 
    wchar_t destination[3]; 
  
    // initialize the source string 
    wchar_t source[] = L"GFG"; 
  
    // till number of characters 
    int n = 3; 
  
    // function to copy from source to 
    // destination 
    wmemcpy(destination, source, n); 
  
    wcout << L"Initial string -> " << source << "\n"; 
  
    // print the copied string 
    wcout << L"Final string -> "; 
    for (int i = 0; i < n; i++) 
        putwchar(destination[i]); 
  
    return 0; 
}
输出:
Initial string -> GFG
Final string -> GFG


相关用法


注:本文由纯净天空筛选整理自AmanSrivastava1大神的英文原创作品 wmemcpy() function in C/C++。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。