std::collate

出自cppreference.com
< cpp‎ | locale
 
 
 
 
 
在標頭 <locale> 定義
template< class CharT >
class collate;

std::collate 封裝本地環境限定的字元串校排(比較)和散列。此刻面為 std::basic_regex 所用,並能以 std::locale::operator() 直接應用到所有期待字元串比較謂詞的標準演算法。

cpp/locale/locale/facetstd-collate-inheritance.svg
關於這幅圖像

繼承圖

目錄

[編輯] 特化

標準庫保證提供以下特化(所有本地環境對象都需要實現這些特化):

在標頭 <locale> 定義
std::collate<char> 實現位元組字元串的字典序定序
std::collate<wchar_t> 實現寬字元串的字典序定序

[編輯] 嵌套類型

類型 定義
char_type CharT
string_type std::basic_string<CharT>

[編輯] 數據成員

成員 描述
std::locale::id id [靜態] 刻面標識

[編輯] 成員函數

構造新的 collate 刻面
(公開成員函數)
銷毀 collate 刻面
(受保護成員函數)
調用 do_compare
(公開成員函數) [編輯]
調用 do_transform
(公開成員函數) [編輯]
調用 do_hash
(公開成員函數) [編輯]

[編輯] 受保護成員函數

用此刻面的校排規則比較兩個字元串
(虛受保護成員函數) [編輯]
變換字元串,使得能用比較替換校排
(虛受保護成員函數) [編輯]
使用此刻面的校排規則生成整數散列值
(虛受保護成員函數) [編輯]

[編輯] 示例

#include <algorithm>
#include <iostream>
#include <locale>
#include <string>
#include <vector>
 
int main()
{
    std::locale::global(std::locale("en_US.utf8"));
    std::wcout.imbue(std::locale(""));
    std::vector<std::wstring> v
    {
        L"ar", L"zebra", L"\u00f6grupp", L"Zebra",
        L"\u00e4ngel",L"\u00e5r", L"f\u00f6rnamn"
    };
 
    std::wcout << "默认本地环境校排顺序:  ";
    std::sort(v.begin(), v.end());
    for (auto s : v)
        std::wcout << s << ' ';
    std::wcout << '\n';
 
    std::wcout << "英文本地环境校排顺序:  ";
    std::sort(v.begin(), v.end(), std::locale("en_US.UTF-8"));
    for (auto s : v)
        std::wcout << s << ' ';
    std::wcout << '\n';
 
    std::wcout << "瑞典文本地环境校排顺序:";
    std::sort(v.begin(), v.end(), std::locale("sv_SE.UTF-8"));
    for (auto s : v)
        std::wcout << s << ' ';
    std::wcout << '\n';
}

輸出:

默认本地环境对照顺序:  Zebra ar förnamn zebra ängel år ögrupp
英文本地环境对照顺序:  ängel ar år förnamn ögrupp zebra Zebra
瑞典文本地环境对照顺序:ar förnamn zebra Zebra år ängel ögrupp

[編輯] 參閱

用此本地環境的校排刻面以字典序比較兩個字元串
(std::locale 的公開成員函數) [編輯]
表示系統提供的具名本地環境的 std::collate
(類模板) [編輯]