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


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


C++ 中的wcscmp() 函数比较两个空终止宽字符串。比较是按字典顺序进行的。

wcscmp() 函数在<cwchar> 头文件中定义。

wcscmp()原型

int wcscmp( const wchar_t* lhs, const wchar_t* rhs );

wcscmp() 函数有两个参数:lhsrhs。它按字典顺序比较lhsrhs 的内容。结果的符号是在 lhsrhs 中不同的第一对字符之间的差异符号。

如果 lhsrhs 中的任何一个不指向空终止的宽字符串,则 wcscmp() 的行为是未定义的。

参数:

  • lhs:指向要比较的空终止宽字符串的指针。
  • rhs:指向要比较的空终止宽字符串的指针。

返回:

wcscmp() 函数返回:

  • 如果 lhs 中的第一个不同字符大于 rhs 中的相应字符,则为正值。
  • 如果 lhs 中的第一个不同字符小于 rhs 中的相应字符,则为负值。
  • 如果 lhsrhs 相等,则为 0。

示例:wcscmp() 函数如何工作?

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

void compare(wchar_t *lhs, wchar_t *rhs)
{
	int result;
	result = wcscmp(lhs, rhs);

	if(result > 0)
		wcout << rhs << " precedes " << lhs << endl;
	else if (result < 0)
		wcout << lhs << " precedes " << rhs << endl;
	else
		wcout << lhs << " and " << rhs << " are same" << endl;
}

int main()
{
	setlocale(LC_ALL, "en_US.utf8");
	
	wchar_t str1[] = L"\u0102\u0070ple";
	wchar_t str2[] = L"\u00c4\u01f7ple";
	wchar_t str3[] = L"\u00c4\u01a4ple";
	
	compare(str1,str2);
	compare(str2,str3);
	
	return 0;
}

运行程序时,输出将是:

ÄǷple precedes Ăpple
ÄƤple precedes ÄǷple

相关用法


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