Namespaces
Variants

strcoll

From cppreference.net
< c ‎ | string ‎ | byte
定义于头文件 <string.h>
int strcoll ( const char * lhs, const char * rhs ) ;

根据由 LC_COLLATE 类别定义的当前本地化设置,比较两个以空字符结尾的字节字符串。

目录

参数

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

返回值

  • lhs 小于 (先于) rhs ,则返回负值。
  • lhs 等于 rhs ,则返回 0
  • lhs 大于 (后于) rhs ,则返回正值。

注释

排序规则采用字典顺序:字母在民族字母表中的位置(其 等价类 )优先于其大小写或变体。在等价类内部,小写字符排序在大写等价字符之前,对于带变音符号的字符可能采用区域特定的顺序。在某些区域设置中,字符组作为单个 排序单元 进行比较。例如,捷克语中的 "ch" 排在 "h" 之后、 "i" 之前,而匈牙利语中的 "dzs" 排在 "dz" 之后、 "g" 之前。

示例

#include <locale.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
    setlocale(LC_COLLATE, "cs_CZ.utf8");
    // 或者,ISO-8859-2(又称 Latin-2)
    // 在某些操作系统上可能也适用:
    // setlocale(LC_COLLATE, "cs_CZ.iso88592");
    const char* s1 = "hrnec";
    const char* s2 = "chrt";
    printf("In the Czech locale: ");
    if (strcoll(s1, s2) < 0)
        printf("%s before %s\n", s1, s2);
    else
        printf("%s before %s\n", s2, s1);
    printf("In lexicographical comparison: ");
    if (strcmp(s1, s2) < 0)
        printf("%s before %s\n", s1, s2);
    else
        printf("%s before %s\n", s2, s1);
}

输出:

In the Czech locale: hrnec before chrt
In lexicographical comparison: chrt before hrnec

参考文献

  • C23 标准 (ISO/IEC 9899:2024):
  • 7.24.4.3 strcoll 函数 (p: TBD)
  • C17 标准 (ISO/IEC 9899:2018):
  • 7.24.4.3 strcoll 函数 (p: 待定)
  • C11 标准 (ISO/IEC 9899:2011):
  • 7.24.4.3 strcoll 函数 (p: 366)
  • C99标准(ISO/IEC 9899:1999):
  • 7.21.4.3 strcoll函数(页码:329)
  • C89/C90 标准 (ISO/IEC 9899:1990):
  • 4.11.4.3 strcoll 函数

参阅

(C95)
根据当前区域设置比较两个宽字符串
(函数)
转换字符串使得 strcmp 能产生与 strcoll 相同的结果
(函数)
(C95)
转换宽字符串使得 wcscmp 能产生与 wcscoll 相同的结果
(函数)
比较两个字符串
(函数)
C++ 文档 关于 strcoll