C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了c – std :: string的区域设置相关顺序大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图以区域设置相关的方式比较std :: Strings.

对于普通的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,s4)) << "\n"; //Outputs 1,like it should

    return 0;
}

但是,我也想为std :: String这样做.我可以只是重载操作符<做某事

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

但是我不得不担心使用std :: less和std :: String :: compare的代码,所以感觉不对.

有没有办法使这种排序规则以无缝的方式进行字符串的工作?

解决方法

std :: locale的operator()只是你正在搜索内容.要获取当前的全局区域设置,只需使用认构造函数即可.

大佬总结

以上是大佬教程为你收集整理的c – std :: string的区域设置相关顺序全部内容,希望文章能够帮你解决c – std :: string的区域设置相关顺序所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。