名稱空間
變體
操作

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 [靜態] facet 的識別符號

[編輯] 成員函式

構造一個新的collate切面
(公開成員函式)
銷燬一個collate切面
(受保護成員函式)
呼叫do_compare
(公有成員函式) [編輯]
呼叫do_transform
(公有成員函式) [編輯]
呼叫do_hash
(公有成員函式) [編輯]

[編輯] 受保護成員函式

使用此 facet 的排序規則比較兩個字串
(虛保護成員函式) [編輯]
轉換字串,以便整理可以透過比較來替代
(虛保護成員函式) [編輯]
使用此切面的整理規則生成整數雜湊值
(虛保護成員函式) [編輯]

[編輯] 示例

#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 << "Default locale collation order: ";
    std::sort(v.begin(), v.end());
    for (auto s : v)
        std::wcout << s << ' ';
    std::wcout << '\n';
 
    std::wcout << "English locale collation order: ";
    std::sort(v.begin(), v.end(), std::locale("en_US.UTF-8"));
    for (auto s : v)
        std::wcout << s << ' ';
    std::wcout << '\n';
 
    std::wcout << "Swedish locale collation order: ";
    std::sort(v.begin(), v.end(), std::locale("sv_SE.UTF-8"));
    for (auto s : v)
        std::wcout << s << ' ';
    std::wcout << '\n';
}

輸出

Default locale collation order: Zebra ar förnamn zebra ängel år ögrupp
English locale collation order: ängel ar år förnamn ögrupp zebra Zebra
Swedish locale collation order: ar förnamn zebra Zebra år ängel ögrupp

[編輯] 另請參閱

使用此區域設定的 collate facet 對兩個字串進行字典比較
(std::locale的公有成員函式) [編輯]
表示命名區域設定的系統提供的std::collate
(類模板) [編輯]