• 字符串排序


    #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("");
    }
     

  • 相关阅读:
    Kustomize 生产实战-注入监控 APM Agent
    2、phpstudy本地搭建网站
    深入解析:微软Edge浏览器的防钓鱼与恶意软件保护机制
    Jmeter性能测试 —— jmeter之使用ServerAgent监控服务器
    微软宣布 S2C2F 已被 OpenSSF 采用
    给出n个学生的考试成绩表,每条信息由姓名与分数组成,试设计一算法:
    springMvc24-Eclipse设置代码自动提示
    替代MySQL半同步复制,Meta技术团队推出MySQL Raft共识引擎
    STM8应用笔记1.基本程序与启动代码分析
    【Python自动化】Python-docx基础入门--插入table表格样式设置
  • 原文地址:https://blog.csdn.net/syb198810/article/details/127107805