• 字符串排序


    #include
    #include
    #include
    #include
    #include

    #include

    #include

    struct NumberStringCompare
    {
        bool operator()(const std::string& a, const std::string& b)
        {
            auto split_str = [](const std::string& complex, std::string& prefix, std::string& suffix, bool is_num) -> void {
                const static std::regex r_get_number(R"([\D])");
                const static std::regex r_get_neg_number(R"([\d])");

                const std::regex& reg = is_num ? r_get_number : r_get_neg_number;

                std::cmatch m;
                if (std::regex_search(complex.c_str(), m, reg))
                {
                    prefix = m.prefix();
                    suffix = m.str() + m.suffix().str();
                }
                else
                {
                    prefix = complex;
                    suffix = "";
                }
            };

            std::string pa, pb, sa(a), sb(b);
            while (!sa.empty()||!sb.empty())
            {
                split_str(sa, pa, sa, true);
                split_str(sb, pb, sb, true);

                if (pa != pb)
                {
                    if (pa.empty()) return sa.empty();
                    if (pb.empty()) return !sb.empty();

                    if (pa.size() != pb.size())
                    {
                        std::string& p = pa.size() < pb.size() ? pa : pb;
                        for (decltype(p.size()) i = 0; i < std::abs((long long)(pa.size() - pb.size())); ++i)
                            p = "0" + p;
                    }

                    if (pa != pb)
                        return pa < pb;
                }

                split_str(sa, pa, sa, false);
                split_str(sb, pb, sb, false);

                CStringA str_pa(pa.c_str());
                auto ret = str_pa.CompareNoCase(pb.c_str());
                if (ret != 0)
                    return ret < 0;
            }

            return true;
        }
    };

    int main()
    {
        std::vector strs({ "abc","ABCD","3","2", "AB","abcde", "10", "01", "abc10", "abc2" });
        std::sort(strs.begin(), strs.end(), NumberStringCompare());
        std::sort(strs.rbegin(), strs.rend(), NumberStringCompare());

        OutputDebugStringA("");
    }
     

  • 相关阅读:
    机器学习中的核方法
    ARFoundation目录指引
    关于knike4j接口文档信息泄露的处理记录
    麦芽糖-链霉亲和素maltose-Streptavidins链霉亲和素-PEG-麦芽糖
    2022杭电多校 第一场 个人题解(ABCDIHK)
    基于element-ui封装可配置表单组件
    MySQL 中 count() 和 count(1) 有什么区别?哪个性能最好?
    一文了解 Go 标准库 strings 常用函数和方法
    SQL的事务隔离属性
    MyBatis-Plus 实现多租户管理的实践
  • 原文地址:https://blog.csdn.net/syb198810/article/details/127107805