CppDS.com

C++ 98 11 14 17 20 手册

wcscmp

来自cppreference.com
< c‎ | string‎ | wide
定义于头文件 <wchar.h>
int wcscmp( const wchar_t *lhs, const wchar_t *rhs );
(C95 起)

以字典序比较二个空终止宽字符串。

结果的符号是被比较的字符串中,首对相异宽字符间的差值符号。

lhsrhs 不是指向空终止宽字符串的指针,则行为未定义。

参数

lhs, rhs - 指向待比较的空终止宽字符串的指针

返回值

若字典序中 lhs 先出现于 rhs 则为负值。

lhsrhs 比较相等则为零。

若字典序中 lhs 后出现于 rhs 则为负值。

注意

不同于 wcscoll ,此函数不考虑本地环境。而且在一同使用来自不同的 Unicode 块的字符时,或编码单元不匹配任何对照顺序时,顺序可能无意义。

示例

#include <wchar.h>
#include <stdio.h>
#include <locale.h>
 
void demo(const wchar_t* lhs, const wchar_t* rhs)
{
    int rc = wcscmp(lhs, rhs);
    const char *rel = rc < 0 ? "precedes" : rc > 0 ? "follows" : "equals";
 
    setlocale(LC_ALL, "en_US.utf8");
    printf("[%ls] %s [%ls]\n", lhs, rel, rhs);
}
 
int main(void)
{
    const wchar_t* string = L"どうもありがとうございます";
    demo(string, L"どうも");
    demo(string, L"助かった");
    demo(string + 9, L"ありがとうございます" + 6);
}

可能的输出:

[どうもありがとうございます] follows [どうも]
[どうもありがとうございます] precedes [助かった]
[ざいます] equals [ざいます]

引用

  • C11 standard (ISO/IEC 9899:2011):
  • 7.29.4.4.1 The wcscmp function (p: 433)
  • C99 standard (ISO/IEC 9899:1999):
  • 7.24.4.4.1 The wcscmp function (p: 379)

参阅

比较来自两个宽字符串的一定量字符
(函数)
比较两个数组中一定数量的宽字符
(函数)
比较两个字符串
(函数)
根据当前本地环境比较两个宽字符串
(函数)
关闭