2011-01-06 9 views
1

私は私の文字列、boolペアベクトルを並べ替えるために使用したいクラスがあります。私の文字列はutf-8でエンコードされています。std :: localeの使用に関するヘルプ?

zap 
    apple 
    école 
    blue 
    erable 

それがために整理することを::人のロケールは、例えばに設定されている場合、私は、私は、ユーザーが入力した場合ことを望んでいるだろう、フランス語、なるようにそれらをソートしたい

apple 
blue 
école 
erable 
zap 

class AguiLBLocaleCompare : 
    public std::binary_function<std::pair<std::string, bool>, 
    std::pair<std::string,bool>, bool> { 
protected: 
    const std::collate<char> &coll; 
public: 
    AguiLBLocaleCompare(std::locale loc) 
     : coll(std::use_facet<std::collate<char> >(loc)) {} 
    bool operator()(const std::pair<std::string, bool> &a, 
     const std::pair<std::string, bool> &b) const { 
     // std::collate::compare() takes C-style string (begin, end)s and 
     // returns values like strcmp or strcoll. Compare to 0 for results 
     // expected for a less<>-style comparator. 
     return coll.compare(a.first.c_str(), a.first.c_str() + a.first.size(), 
      b.first.c_str(), b.first.c_str() + b.first.size()) < 0; 
    } 
}; 

して、ソート方法私の項目は次のとおりです:

私のstd ::ロケールのクラスが、これはある

void AguiListBox::sort() 
{ 
    if(!isReverseSorted()) 
     std::sort(items.begin(),items.end(),AguiLBLocaleCompare(WHAT_DO_I_PUT_HERE)); 
    else 
     std::sort(items.rbegin(),items.rend(),AguiLBLocaleCompare(WHAT_DO_I_PUT_HERE)); 
} 

私は希望の効果を得るためにコンストラクタをどのように配置するのかよく分かりません。

私はstd::locale()を試しましたが、私が望んでいないzapの後にアクセントをソートしました。

項目が

std::vector<std::pair<std::string, bool>> 

おかげ

+0

あなたはどのプラットフォームにいますか? C++のロケールのサポートはうっかりしています。 –

+0

私はWindows 7にいる – jmasterx

+0

構文上、そこに任意のロケールを置くことができます。 'std :: locale :: locale(" fr_FR.utf8 ")'しかし、私はどのロケールのWindowsが利用できるのか、そのC++ロケール名が何であるか分かりません。 –

答えて

1

である私は、VC++は、UTF-8ロケールをサポートしていないと思います。おそらくconvertwstringで、collate<wchar_t>を使用するか、UTF-8ロケールをサポートするC++ライブラリに切り替えてください。

Windows/VC++のロケール名は、UNIXの場合と異なります。 MSDNのLanguage and Country/Region Strings (CRT)を参照してください。

関連する問題