名稱空間
變體
操作

std::strcoll

來自 cppreference.com
< cpp‎ | string‎ | byte
在標頭檔案 <cstring> 中定義
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 <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

[編輯] 參閱

根據當前語言環境比較兩個寬字串
(函式) [編輯]
[virtual]
使用此 facet 的排序規則比較兩個字串
(std::collate<CharT> 的虛保護成員函式) [編輯]
轉換字串,使 strcmp 產生與 strcoll 相同的結果
(函式) [編輯]
C 文件 對應 strcoll