gpt4 book ai didi

c++ - 帮助使用 std::locale?

转载 作者:太空宇宙 更新时间:2023-11-04 12:21:43 24 4
gpt4 key购买 nike

我有一个类,我想用它来对我的字符串 bool 对 vector 进行排序。我的字符串是 utf-8 编码的。我想对它们进行排序,以便如果此人的语言环境设置为法语,我希望如果用户键入:

    zap
apple
école
blue
erable

结果会是:

apple
blue
école
erable
zap

我的 std::locale 类是这样的:

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;
}
};

然后我的元素排序方法是:

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 中将重音排序在 z 之后,这不是我想要的。

项目是一个

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

谢谢

最佳答案

我认为 VC++ 不支持 UTF-8 语言环境。你可能应该 convertwstring并使用 collate<wchar_t> ,或切换到支持 UTF-8 语言环境的 C++ 库。

Windows/VC++ 上的区域设置名称与 UNIX 上的不同;见Language and Country/Region Strings (CRT)在 MSDN 上。

关于c++ - 帮助使用 std::locale?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4618265/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com