c++ 多种字符串排序使用

情形一(按字典序与长度增序排序)

  • 得到一组字符串时,要求增序排序先比较长度,长度一致时比较字典序,字典序一致时再比较后面的数字
  • 示例:{"hello", "a2", "bw6", "hello1", "h3llo", "aa1", "c", "dasd2", "aa3"}经过排序后为,("c", "a2", "aa1", "aa3", "bw6", "dasd2", "h3llo", "hello", "hello1")
    QList<QString> lists = {"hello", "a2", "bw6", "hello1", "h3llo", "aa1", "c", "dasd2", "aa3"};
    //("c", "a2", "aa1", "aa3", "bw6", "dasd2", "h3llo", "hello", "hello1")
    std::sort(lists.begin(), lists.end(), [=](QString &s1, QString &s2)
              {
        // 比较字符串长度
        if (s1.length() != s2.length()) {
            return s1.length() < s2.length();
        }
        // 长度相同,比较字典序
        int result = s1.localeAwareCompare(s2);
        //int result = localeAwareCompare(s1.toStdString(), s2.toStdString());
        if (result != 0) {
            return result < 0;
        }
        // 字典序相同,比较数字部分
        QRegularExpression re("\\d+");
        QRegularExpressionMatch match1 = re.match(s1);
        QRegularExpressionMatch match2 = re.match(s2);
        // 如果两个字符串都包含数字,则比较数字
        if (match1.hasMatch() && match2.hasMatch()) {
            int number1 = match1.captured().toInt();
            int number2 = match2.captured().toInt();
            return number1 < number2;
        }
        // 如果只有一个字符串包含数字,则包含数字的字符串排在后面
        return match1.hasMatch() ? false : true; });
  • 上述代码以qt为例,localeAwareCompare函数为QT特有用于比较字典序。在标准的 C++ 中,并没有直接等价的功能,因此要实现类似于 Qt 的 localeAwareCompare 函数,你需要依赖 C++ 库自己实现。
#include <string>
#include <locale>
int localeAwareCompare(const std::string& s1, const std::string& s2) {
    // 获取默认locale
    const std::locale& loc = std::locale();
    // 使用locale的collate比较两个字符串
    const std::collate<char>& coll = std::use_facet<std::collate<char>>(loc);
    return coll.compare(s1.data(), s1.data() + s1.size(), s2.data(), s2.data() + s2.size());
}
  • 在上面的代码中,localeAwareCompare 函数使用默认的 locale (std::locale()) 来获取 collate 对象,这个对象负责处理字符串的比较。collate::compare 函数用于比较范围内的两个字符串。

情形二(去重后排序)

  • **去重问题很好解决,使用set**容器就可以实现了。
    // 使用QSet去重
    QSet<QString> uniqueSet(list.begin(), list.end());
    QList<QString> uniqueList(uniqueSet.begin(), uniqueSet.end());
    // 使用自定义比较函数进行排序
    std::sort(uniqueList.begin(), uniqueList.end(), customLessThan);
  • customLessThan即将情形一的排序函数封装一下。

情形三(待续)

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
除了使用哈希表和双指针的方法,还可以使用其他方法来实现字符串去重。下面介绍几种常见的方法: 1. 排序去重法 将字符串排序后,遍历字符串中的每个字符,如果该字符不等于前一个字符,则将该字符加入结果字符串中。这种方法的时间复杂度为 O(nlogn),空间复杂度为 O(n)。 代码如下: ```c++ #include <iostream> #include <string> #include <algorithm> using namespace std; string remove_duplicate_chars(string s) { sort(s.begin(), s.end()); string result; for (int i = 0; i < s.size(); i++) { if (i == 0 || s[i] != s[i-1]) { result += s[i]; } } return result; } int main() { string s = "abcaabbcc"; string result = remove_duplicate_chars(s); cout << result << endl; // 输出:abc return 0; } ``` 2. 暴力枚举法 遍历字符串中的每个字符,判断该字符在字符串中是否重复出现,如果没有则将该字符加入结果字符串中。这种方法的时间复杂度为 O(n^2),空间复杂度为 O(n)。 代码如下: ```c++ #include <iostream> #include <string> using namespace std; string remove_duplicate_chars(string s) { string result; for (int i = 0; i < s.size(); i++) { bool found = false; for (int j = 0; j < i; j++) { if (s[j] == s[i]) { found = true; break; } } if (!found) { result += s[i]; } } return result; } int main() { string s = "abcaabbcc"; string result = remove_duplicate_chars(s); cout << result << endl; // 输出:abc return 0; } ``` 3. 位图法 使用一个长度为 256 的布尔型数组,将字符串中每个字符的 ASCII 码作为数组下标,并将对应的数组元素置为 true。这种方法的时间复杂度为 O(n),空间复杂度为 O(1)。 代码如下: ```c++ #include <iostream> #include <string> using namespace std; string remove_duplicate_chars(string s) { bool bitmap[256] = {false}; string result; for (int i = 0; i < s.size(); i++) { int index = (int)s[i]; if (!bitmap[index]) { bitmap[index] = true; result += s[i]; } } return result; } int main() { string s = "abcaabbcc"; string result = remove_duplicate_chars(s); cout << result << endl; // 输出:abc return 0; } ``` 需要注意的是,这种方法只能处理 ASCII 码范围内的字符,无法处理 Unicode 码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值