【问题标题】:locale-dependent ordering for std::stringstd::string 的语言环境相关排序
【发布时间】:2009-08-31 13:05:07
【问题描述】:

我正在尝试以依赖于语言环境的方式比较 std::strings。

对于普通的 C 风格字符串,我找到了 strcoll,在完成 std::setlocale 之后,它完全符合我的要求

#include <iostream>
#include <locale>
#include <cstring>

bool cmp(const char* a, const char* b)
{
    return strcoll(a, b) < 0;
}

int main()
{
    const char* s1 = "z", *s2 = "å", *s3 = "ä", *s4 = "ö";

    std::cout << (cmp(s1,s2) && cmp(s2,s3) && cmp(s3,s4)) << "\n"; //Outputs 0
    std::setlocale(LC_ALL, "sv_SE.UTF-8");
    std::cout << (cmp(s1,s2) && cmp(s2,s3) && cmp(s3,s4)) << "\n"; //Outputs 1, like it should

    return 0;
}

但是,我也希望 std::string 也有这种行为。我可以重载operator&lt; 来做类似的事情

bool operator<(const std::string& a, const std::string& b)
{
    return strcoll(a.c_str(), b.c_str());
}

但是我不得不担心使用std::lessstd::string::compare 的代码,所以感觉不对。

有没有办法让这种排序规则无缝地适用于字符串?

【问题讨论】:

    标签: c++ string locale


    【解决方案1】:

    operator() of std::locale 正是您要搜索的内容。要获取当前的全局语言环境,只需使用默认构造函数即可。

    【讨论】:

    • 这很方便。它使标准集合毫不费力地工作。
    【解决方案2】:

    C++ 库提供collate facet 来进行特定于语言环境的排序。

    【讨论】:

    • locale 上的 operator() 是我知道的最简单的访问方法。
    【解决方案3】:

    经过一番搜索后,我意识到一种方法是重载std::basic_string 模板以创建一个新的本地化字符串类。

    这其中可能存在大量错误,但作为概念证明:

    #include <iostream>
    #include <locale>
    #include <string>
    
    struct localed_traits: public std::char_traits<wchar_t>
    {
        static bool lt(wchar_t a, wchar_t b)
        {
            const std::collate<wchar_t>& coll =
                std::use_facet< std::collate<wchar_t> >(std::locale());
            return coll.compare(&a, &a+1, &b, &b+1) < 0;
        }
    
        static int compare(const wchar_t* a, const wchar_t* b, size_t n)
        {
            const std::collate<wchar_t>& coll =
                std::use_facet< std::collate<wchar_t> >(std::locale());
            return coll.compare(a, a+n, b, b+n);
        }
    };
    
    typedef std::basic_string<wchar_t, localed_traits> localed_string;
    
    int main()
    {
        localed_string s1 = L"z", s2 = L"å", s3 = L"ä", s4 = L"ö";
    
        std::cout << (s1 < s2 && s2 < s3 && s3 < s4 ) << "\n"; //Outputs 0
        std::locale::global(std::locale("sv_SE.UTF-8"));
        std::cout << (s1 < s2 && s2 < s3 && s3 < s4 ) << "\n"; //Outputs 1
    
        return 0;
    }
    

    但是,如果您基于char 而不是wchar_t,它似乎不起作用,我不知道为什么......

    【讨论】:

    • char 不起作用的原因是它没有使用 unicode(如“.UTF-8”)。您可能使用的是 ISO/IEC 8859-1。
    • &amp;a+1 应该做什么?
    【解决方案4】:

    在 C++ 中,您需要使用标准的 collat​​e 方面。 Check it out.

    【讨论】: