2009-08-31 56 views
9

我試圖以區域設置相關的方式比較std::stringstd :: string的區域設置相關排序

對於普通的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<做這樣的事情

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

但後來我不得不擔心使用std::lessstd::string::compare代碼,因此它感覺不對。

有沒有辦法讓這種排序方式爲字符串以無縫方式工作?

回答

7

std :: locale的operator()就是你正在搜索的東西。要獲取當前的全局語言環境,只需使用默認的構造函數即可。

+0

這很方便。它使標準館藏無需工作。 – CAdaker 2009-08-31 14:12:04

7

C++庫提供collate facet以執行特定於語言環境的歸類。

+0

locale上的operator()是我知道訪問它的最簡單的方法。 – AProgrammer 2009-08-31 13:26:20

+1

我明白了 - 我不知道。 – 2009-08-31 13:48:31

0

經過一番探索後,我意識到一種方法可能是重載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; 
} 

Howerver,它似乎沒有,如果你將它基於char而不是wchar_t工作,我不知道爲什麼...

+0

char不工作的原因是它沒有使用unicode(就像在「.UTF-8」中一樣。你可能使用ISO/IEC 8859-1。 – 2009-08-31 20:48:55

+0

'&a + 1'應該做什麼? – 0x499602D2 2013-07-31 12:59:31