Namespaces
Variants

std:: wcscoll

From cppreference.net
定义于头文件 <cwchar>
int wcscoll ( const wchar_t * lhs, const wchar_t * rhs ) ;

根据最近一次由 std::setlocale 设置的locale,按照 LC_COLLATE 类别定义的方式比较两个以空字符结尾的宽字符串。

目录

参数

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

返回值

lhs 小于 (先于) rhs ,则返回负值。

0 如果 lhs 等于 rhs

lhs 大于 (在排序中位于) rhs 时返回正值。

注释

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

示例

#include <clocale>
#include <iostream>
void try_compare(const wchar_t* p1, const wchar_t* p2)
{
    if (std::wcscoll(p1, p2) < 0)
        std::wcout << p1 << " before " << p2 << '\n';
    else
        std::wcout << p2 << " before " << p1 << '\n';
}
int main()
{
    std::setlocale(LC_ALL, "en_US.utf8");
    std::wcout << "In the American locale: ";
    try_compare(L"hrnec", L"chrt");
    std::setlocale(LC_COLLATE, "cs_CZ.utf8");
    std::wcout << "In the Czech locale: ";
    try_compare(L"hrnec", L"chrt");
    std::setlocale(LC_COLLATE, "en_US.utf8");
    std::wcout << "In the American locale: ";
    try_compare(L"år", L"ängel");
    std::setlocale(LC_COLLATE, "sv_SE.utf8");
    std::wcout << "In the Swedish locale: ";
    try_compare(L"år", L"ängel");
}

输出:

In the American locale: chrt before hrnec
In the Czech locale: hrnec before chrt
In the American locale: ängel before år
In the Swedish locale: år before ängel

参见

根据当前区域设置比较两个字符串
(函数)
[virtual]
使用此locale facet的排序规则比较两个字符串
( std::collate<CharT> 的虚函数保护成员)
转换宽字符串,使 wcscmp 能产生与 wcscoll 相同的结果
(函数)
C 文档 关于 wcscoll