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


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


C /C++中的wmemcmp()函数比较两个宽字符。此函数比较由str1和str2指向的两个宽字符的前num个宽字符,如果两个字符串相等或不同,则返回零(如果不是)。

用法:

int wmemcmp (const wchar_t* str1, const wchar_t* str2, size_t num);



参数:

  • str1: 指定指向第一个字符串的指针。
  • str2: 指定指向第二个字符串的指针。
  • num: 指定要比较的字符数。

返回值:此函数返回三个不同的值,这些值定义了两个字符串之间的关系:

  • :当两个字符串相等时。
  • 正值:两个字符串中不匹配的第一个宽字符在str1中的频率高于在str2中的频率。
  • 负值:当两个字符串中不匹配的第一个宽字符在str1中的频率低于在str2中的频率

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

示例1:

// C++ program to illustrate 
// wmemcmp() function 
  
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
    // initialize two strings 
    wchar_t str1[] = L"geekforgeeks"; 
    ; 
    wchar_t str2[] = L"geekforgeeks"; 
  
    // this function will compare these two strings 
    // till 12 characters, if there would have been 
    // more than 12 characters, it will compare 
    // even more than the length of the strings 
    int print = wmemcmp(str1, str2, 12); 
  
    // print if it's equal or not equal( greater or smaller) 
    wprintf(L"wmemcmp comparison: %ls\n", 
            print ? L"not equal" : L"equal"); 
  
    return 0; 
}
输出:
wmemcmp comparison: equal

示例2:

// C++ program to illustrate 
// wmemcmp() function 
// Comparing two strings with the same type of function 
// wcsncmp() and wmemcmp() 
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
    // initialize two strings 
    wchar_t str1[] = L"geekforgeeks"; 
    ; 
    wchar_t str2[] = L"geekforgeeks"; 
  
    // wcsncmp() function compare characters 
    // until the null character is encountered ('\0') 
    int first = wcsncmp(str1, str2, 20); 
  
    // but wmemcmp() function compares 20 characters 
    // even after encountering null character 
    int second = wmemcmp(str1, str2, 20); 
  
    // print if it's equal or not equal( greater or smaller) 
    wprintf(L"wcsncmp comparison: %ls\n", 
            first ? L"not equal" : L"equal"); 
    wprintf(L"wmemcmp comparison: %ls\n", 
            second ? L"not equal" : L"equal"); 
  
    return 0; 
}
输出:
wcsncmp comparison: equal
wmemcmp comparison: not equal


相关用法


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