std::string
ロケールに依存する方法で sを比較しようとしています。
通常の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
しかし、その後、 andを使用するコードについて心配する必要がstd::string::compare
あるため、適切ではありません。
この種の照合を文字列に対してシームレスに機能させる方法はありますか?