命名空间
变体
操作

std::strcoll

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

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

目录

[编辑] 参数

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

[编辑] 返回值

  • 如果 lhs *小于*(先于)rhs,则为负值。
  • 0 如果 lhs *等于* rhs,则为零。
  • 如果 lhs *大于*(后于)rhs,则为正值。

[编辑] 说明

排序规则是字典序:字母在国家字母表中的位置(其等价类)比其大小写或变体具有更高的优先级。在等价类中,小写字符的排序先于其大写等价物,并且特定于区域设置的顺序可能适用于带有变音符号的字符。在某些区域设置中,字符组作为单个排序单元进行比较。例如,捷克语中的 "ch""h" 之后,在 "i" 之前;匈牙利语中的 "dzs""dz" 之后,在 "g" 之前。

[编辑] 示例

#include <clocale>
#include <cstring>
#include <iostream>
 
int main()
{
    std::setlocale(LC_COLLATE, "cs_CZ.utf8");
    // Alternatively, ISO-8859-2 (a.k.a. Latin-2)
    // may also work on some OS:
    // std::setlocale(LC_COLLATE, "cs_CZ.iso88592");
 
    const char* s1 = "hrnec";
    const char* s2 = "chrt";
 
    std::cout << "In the Czech locale: ";
    if (std::strcoll(s1, s2) < 0)
        std::cout << s1 << " before " << s2 << '\n';
    else
        std::cout << s2 << " before " << s1 << '\n';
 
    std::cout << "In lexicographical comparison: ";
    if (std::strcmp(s1, s2) < 0)
        std::cout << s1 << " before " << s2 << '\n';
    else
        std::cout << s2 << " before " << s1 << '\n';
}

输出

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

[编辑] 参见

根据当前区域设置比较两个宽字符串
(函数) [编辑]
[虚拟]
使用此 facet 的排序规则比较两个字符串
(std::collate<CharT> 的虚拟保护成员函数) [编辑]
转换字符串,使得 strcmp 产生与 strcoll 相同的结果
(函数) [编辑]