命名空间
变体
操作

strcoll

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

根据当前区域设置(由 LC_COLLATE 类别定义)比较两个空终止字节字符串。

目录

[编辑] 参数

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

[编辑] 返回值

  • 如果 lhs 小于(先于) rhs,则为负值。
  • 0 如果 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");
    // Alternatively, ISO-8859-2 (a.k.a. Latin-2)
    // may also work on some OS:
    // 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 函数 (页码:待定)
  • C17 标准 (ISO/IEC 9899:2018)
  • 7.24.4.3 strcoll 函数 (页码:待定)
  • C11 标准 (ISO/IEC 9899:2011)
  • 7.24.4.3 strcoll 函数 (页码:366)
  • C99 标准 (ISO/IEC 9899:1999)
  • 7.21.4.3 strcoll 函数 (页码:329)
  • C89/C90 标准 (ISO/IEC 9899:1990)
  • 4.11.4.3 strcoll 函数

[编辑] 参见

根据当前区域设置比较两个宽字符串
(函数) [编辑]
转换字符串,以便 strcmp 产生与 strcoll 相同的结果
(函数) [编辑]
转换宽字符串,以便 wcscmp 产生与 wcscoll 相同的结果
(函数) [编辑]
比较两个字符串
(函数) [编辑]
C++ 文档 for strcoll