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
相關用法
- C++ div()用法及代碼示例
- C++ fma()用法及代碼示例
- C++ log()用法及代碼示例
- C++ regex_iterator()用法及代碼示例
- C++ isunordered()用法及代碼示例
- C++ map key_comp()用法及代碼示例
- C++ real()用法及代碼示例
- C++ imag()用法及代碼示例
- C++ valarray pow()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSrivastava1大神的英文原創作品 wmemcmp() function in C/C++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。