vector混合排序(sort + stable_sort)

本文介绍了一种在C++中对保存了多个字符串的vector容器进行排序和去重的方法。首先使用标准库函数sort对vector进行字典排序,然后通过unique去除重复元素,最后使用stable_sort按字符串长度稳定排序。示例代码展示了如何实现这一过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

需求说明:

一个保存多个字符串的vector容器,对其实现排序/去重,并且要求排序的时候先按长度排序,长度相同的再按照字典排序。

示例代码:

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

//按长度排
bool isShorter( const string &strSouce, const string &strDest )
{
    return strSouce.size() < strDest.size();
}

//先按长度排,再按字典排
void Sort_vector( vector<string> &sVec )
{
    sort( sVec.begin(), sVec.end() );
    auto endIter = unique( sVec.begin(), sVec.end() );
    sVec.erase( endIter, sVec.end() );
    stable_sort( sVec.begin(), sVec.end(), isShorter ); //比较函数的参数需要是const类型
}

//by zhaocl
int main()
{
    vector<string> strVec = { "aaa", "ccc", "ccc", "bbbb", "aaaa" };
    Sort_vector( strVec );

    for( auto s : strVec )
    {
        cout << s << endl;
    }

    system( "pause" );
    return 0;
}

结果示图:

### C++ 中 `std::sort` 和 `std::stable_sort` 的区别及用法 #### 函数签名 两个函数都位于 `<algorithm>` 头文件中,用于对容器中的元素进行排序。 对于 `std::sort`: ```cpp template<class RandomIt> void sort(RandomIt first, RandomIt last); template<class RandomIt, class Compare> void sort(RandomIt first, RandomIt last, Compare comp); ``` 对于 `std::stable_sort`: ```cpp template<class RandomIt> void stable_sort(RandomIt first, RandomIt last); template<class RandomIt, class Compare> void stable_sort(RandomIt first, RandomIt last, Compare comp); ``` #### 排序稳定性 主要差异在于稳定性和性能: - **`std::sort`**: 不保证排序算法的稳定性。这意味着如果存在个具有相同键值的元素,则它们之间的相对顺序可能会改变[^1]。 - **`std::stable_sort`**: 保持相等元素间的原始顺序不变。这在处理复杂数据结构时非常重要,尤其是当这些对象有次级排序标准时。 #### 性能比较 通常情况下,`std::sort` 实现为快速排序变体,在平均情况下的时间复杂度接近 O(n log n),而最坏情况下可能退化到 O();不过现代 STL 库往往采用混合策略来避免这种情况发生。 另一方面,为了维持稳定性,`std::stable_sort` 可能会牺牲一些速度优势,其内部实现通常是基于归并排序或其他能够提供稳定性的方法,因此即使是在最优条件下也难以超越 `std::sort` 的效率。 #### 使用示例 下面是一个简单的例子展示如何使用这两个函数以及它们的不同之处: ```cpp #include <iostream> #include <vector> #include <algorithm> struct Item { int id; char name; bool operator<(const Item& other) const { return this->id < other.id; } }; int main() { std::vector<Item> items = {{2,'b'}, {1,'a'}, {3,'c'}, {2,'d'}}; // Using std::sort which does not guarantee stability. std::cout << "After using std::sort:\n"; std::sort(items.begin(), items.end()); for(auto &item : items){ std::cout << item.id << ' '; } std::cout << '\n'; // Resetting vector back to original state before sorting again. items = {{2,'b'}, {1,'a'}, {3,'c'}, {2,'d'}}; // Using std::stable_sort that guarantees stability. std::cout << "\nAfter using std::stable_sort:\n"; std::stable_sort(items.begin(), items.end()); for(auto &item : items){ std::cout << item.id << '(' << item.name << ") "; } return 0; } ``` 上述程序展示了两种不同的排序方式下输出结果的变化,特别是注意观察 ID 值相同的项之间名称字符的位置变化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值