我试图以区域设置相关的方式比较std::string
。std :: 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::less
和std::string::compare
代码,因此它感觉不对。
有没有办法让这种排序方式为字符串以无缝方式工作?
这很方便。它使标准馆藏无需工作。 – CAdaker 2009-08-31 14:12:04