C++ STL中的sort()和stable_sort()函数

1、sort (first, last) : 对容器或普通数组中 [first, last) 范围内的元素进行排序,默认进行升序排序。

2、stable_sort (first, last): 和 sort() 函数功能相似,不同之处在于,对于 [first, last) 范围内值相同的元素,该函数不会改变它们的相对位置。

3、stable_sort() 函数是基于归并排序实现的

4、sort() 函数是基于快速排序实现的

sort()函数的使用例子:

#include <iostream>     // std::cout
#include <algorithm>    // std::stable_sort
#include <vector>       // std::vector
//以普通函数的方式实现自定义排序规则
bool mycomp(int i, int j) {
    
    
    return (i < j);
}
//以函数对象的方式实现自定义排序规则
class mycomp2 {
    
    
public:
    bool operator() (int i, int j) {
    
    
        return (i < j);
    }
};

int main() {
    
    
    std::vector<int> myvector{
    
     32, 71, 12, 45, 26, 80, 53, 33 };
    //调用第一种语法格式,对 32、71、12、45 进行排序
    std::stable_sort(myvector.begin(), myvector.begin() + 4); //(12 32 45 71) 26 80 53 33
    //调用第二种语法格式,利用STL标准库提供的其它比较规则(比如 greater<T>)进行排序
    std::stable_sort(myvector.begin(), myvector.begin() + 4, std::greater<int>()); //(71 45 32 12) 26 80 53 33

    //调用第二种语法格式,通过自定义比较规则进行排序,这里也可以换成 mycomp2()
    std::stable_sort(myvector.begin(), myvector.end(), mycomp);//12 26 32 33 45 53 71 80
    //输出 myvector 容器中的元素
    for (std::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it) {
    
    
        std::cout << *it << ' ';
    }
    return 0;
}

stable_sort()函数的使用例子:

#include <iostream>     // std::cout
#include <algorithm>    // std::stable_sort
#include <vector>       // std::vector
//以普通函数的方式实现自定义排序规则
bool mycomp(int i, int j) {
    
    
    return (i < j);
}
//以函数对象的方式实现自定义排序规则
class mycomp2 {
    
    
public:
    bool operator() (int i, int j) {
    
    
        return (i < j);
    }
};

int main() {
    
    
    std::vector<int> myvector{
    
     32, 71, 12, 45, 26, 80, 53, 33 };
    //调用第一种语法格式,对 32、71、12、45 进行排序
    std::stable_sort(myvector.begin(), myvector.begin() + 4); //(12 32 45 71) 26 80 53 33
    //调用第二种语法格式,利用STL标准库提供的其它比较规则(比如 greater<T>)进行排序
    std::stable_sort(myvector.begin(), myvector.begin() + 4, std::greater<int>()); //(71 45 32 12) 26 80 53 33

    //调用第二种语法格式,通过自定义比较规则进行排序,这里也可以换成 mycomp2()
    std::stable_sort(myvector.begin(), myvector.end(), mycomp);//12 26 32 33 45 53 71 80
    //输出 myvector 容器中的元素
    for (std::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it) {
    
    
        std::cout << *it << ' ';
    }
    return 0;
}

参考链接:

C++ stable_sort()用法详解
C++ sort()排序函数用法详解

谢谢阅读

猜你喜欢

转载自blog.csdn.net/weixin_43869898/article/details/109117102