C++排序函数sort

一、sort函数的用法
  • 必须包含头文件:#include <algorithm>using namespace std;
  • 时间复杂度为:n*log2(n)
  • sort函数有三个参数:(第三个参数可选
    • 第一个是要排序的数组的起始地址
    • 第二个是结束的地址(最后一位要排序的地址)
    • 第三个参数是排序的方法,可以是升序也可是降序。如果不写第三个参数,则默认的排序方法是从升序排列

二、两种用法
两个参数用法(此时默认升序排列)
#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
    int i;
    int a[10] = { 2, 4, 1, 23, 5, 76, 0, 43, 24, 65 };

    for(i = 0; i < 10; i++) {
        cout << a[i] << " ";  //2 4 1 23 5 76 0 43 24 65
    }

    sort(a, a + 10);

    for(i = 0; i < 10; i++) {
        cout << a[i] << " ";  //0 1 2 4 5 23 24 43 65 76
    }

    return 0;
}
三个参数的用法(可选升序或降序)

首先需要自定义一个比较函数:compare()

bool compare(int a, int b) {
    //return a < b;  //升序排列
    return a > b;  //降序排列
}

这个函数告诉程序,到底是升序还是降序排列。

#include <iostream>
#include <algorithm>

using namespace std;

bool compare(int a, int b) {
    //return a < b;  //升序排列
    return a > b;  //降序排列
}

int main()
{
    int i;
    int a[10] = { 2, 4, 1, 23, 5, 76, 0, 43, 24, 65 };

    for(i = 0; i < 10; i++) {
        cout << a[i] << " ";  //2 4 1 23 5 76 0 43 24 65
    }
    cout << endl;

    sort(a, a + 10, compare);  //这里不需要对compare传参,这是规则

    for(i = 0; i < 10; i++) {
        cout << a[i] << " ";  //76 65 43 24 23 5 4 2 1 0
    }

    return 0;
}

也可以不自定义一个比较函数,可以调用标准库里的模板:

这里会用到:functional头文件,所以别忘了加上#include <functional>

functional提供了一堆基于模板的比较函数对象。它们是(看名字就知道意思了):equal_to、not_equal_to、greater、greater_equal、less、less_equal。对于这个问题来说,greater和less就足够了,直接拿过来用:

  • 升序:sort( begin, end, less<data-type>() );
  • 降序:sort( begin, end, greater<data-type>() );
# include <iostream>
# include <algorithm>
# include <functional>

using namespace std;

int main()
{
    int i;
    int a[10] = { 2, 4, 1, 23, 5, 76, 0, 43, 24, 65 };

    for (i = 0; i < 10; i++) {
        cout << a[i] << " ";  //2 4 1 23 5 76 0 43 24 65
    }
    cout << endl;

    //升序
    sort( a, a + 10, less<int>() );

    for (i = 0; i < 10; i++) {
        cout << a[i] << " ";  //0 1 2 4 5 23 24 43 65 76
    }

    cout << endl;

    //降序
    sort( a, a + 10, greater<int>() );

    for (i = 0; i < 10; i++) {
        cout << a[i] << " ";  //76 65 43 24 23 5 4 2 1 0
    }
    cout << endl;

    return 0;
}

三、对字符串型的数据排序

1.使用迭代器,顺序排列:

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

using namespace std;

int main()
{
    string str = "hello world";

    sort( str.begin(), str.end() );

    cout << str;  //空格dehllloorw   //注意空格

    return 0;
 }

2.使用反向迭代器,逆序排列:

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

using namespace std;

int main()
{
    string str = "hello world";

    sort( str.rbegin(), str.rend() );

    cout << str;  //wroolllhed空格  //注意空格

    return 0;
 }



使用IDE:CodeBlocks

结语:文章有错误或不足,欢迎评论,希望轻拍,一起进步哦!

END!

  • 10
    点赞
  • 63
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值