C++ 中的wcscmp() 函数比较两个空终止宽字符串。比较是按字典顺序进行的。
wcscmp() 函数在<cwchar> 头文件中定义。
wcscmp()原型
int wcscmp( const wchar_t* lhs, const wchar_t* rhs );
wcscmp() 函数有两个参数:lhs
和 rhs
。它按字典顺序比较lhs
和rhs
的内容。结果的符号是在 lhs
和 rhs
中不同的第一对字符之间的差异符号。
如果 lhs
或 rhs
中的任何一个不指向空终止的宽字符串,则 wcscmp() 的行为是未定义的。
参数:
lhs
:指向要比较的空终止宽字符串的指针。rhs
:指向要比较的空终止宽字符串的指针。
返回:
wcscmp() 函数返回:
- 如果
lhs
中的第一个不同字符大于rhs
中的相应字符,则为正值。 - 如果
lhs
中的第一个不同字符小于rhs
中的相应字符,则为负值。 - 如果
lhs
和rhs
相等,则为 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()用法及代码示例
- C++ wcscspn()用法及代码示例
- C++ wcschr()用法及代码示例
- C++ wcscat()用法及代码示例
- C++ wcscoll()用法及代码示例
- C++ wcscpy()用法及代码示例
- C++ wcstold()用法及代码示例
- C++ wcsftime()用法及代码示例
- C++ wcstod()用法及代码示例
- C++ wcsncmp()用法及代码示例
- C++ wcstok()用法及代码示例
- C++ wcsstr()用法及代码示例
- C++ wcsrchr()用法及代码示例
- C++ wcsncpy()用法及代码示例
- C++ wcslen()用法及代码示例
- C++ wcsspn()用法及代码示例
- C++ wcstof()用法及代码示例
- C++ wcstol()用法及代码示例
- C++ wcspbrk()用法及代码示例
- C++ wcsncat()用法及代码示例
注:本文由纯净天空筛选整理自 C++ wcscmp()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。