當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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

C++ 中的wmemcmp() 函數比較兩個寬字符串的指定數量的寬字符。

wmemcmp() 函數在<cwchar> 頭文件中定義。

wmemcmp()原型

int wmemcmp( const wchar_t* lhs, const wchar_t* rhs, size_t count );

wmemcmp() 函數采用三個參數:lhs , rhscount。此函數按字典順序比較lhsrhs 的第一個計數寬字符。

參數:

  • lhsrhs :指向要比較的寬字符數組的指針。
  • count:要比較的最大寬字符數。

返回:

wmemcmp() 函數返回:

  • 如果 lhs 中的第一個不同的寬字符大於 rhs 中的相應字節,則為正值。
  • 如果 lhs 中的第一個不同的寬字符小於 rhs 中的相應字節,則為負值。
  • 如果 lhs 和 rhs 的第一個 count 寬字符相等,則為 0

示例:wmemcmp() 函數如何工作?

#include <cwchar>
#include <clocale>
#include <iostream>
using namespace std;

void compare(wchar_t *lhs, wchar_t *rhs, int count)
{
	int result = wmemcmp(lhs, rhs, count);
	
	if(result > 0)
		wcout << rhs << L" precedes " << lhs << endl;
	else if (result < 0)
		wcout << lhs << L" precedes " << rhs << endl;
	else
		wcout << L"First " << count << L" characters of " << lhs << L" and " << rhs << L" are same" << endl;
}

int main()
{
	setlocale(LC_ALL, "en_US.utf8");

	wchar_t lhs[] = L"\u0386\u03a6\u03aa\u03ac\u03c8\u03c9\u03ee";
	wchar_t rhs[] = L"\u0386\u03a6\u03aa\u03ac\u03c0\u03c7\u03fb";
	
	compare(lhs, rhs, 4);
	compare(lhs, rhs, 7);
	
	return 0;
}

運行程序時,輸出將是:

First 4 characters of ΆΦΪάψωϮ and ΆΦΪάπχϻ are same
ΆΦΪάπχϻ precedes ΆΦΪάψωϮ

相關用法


注:本文由純淨天空篩選整理自 C++ wmemcmp()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。