[C++] 基本教程 - std::sort函数介绍和使用场景

std::sort是C++标准库中的一个函数,用于对容器中的元素进行排序。它可以按照默认的升序方式对元素进行排序,也可以指定自定义的比较函数来实现降序排序等其他排序方式。

函数介绍

std::sort函数位于<algorithm>头文件中,其原型如下:

template <class RandomAccessIterator, class Compare>
void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);

其中,firstlast表示待排序序列的起始和结束迭代器,comp是一个可选的比较函数,用于指定排序规则。如果不提供比较函数,则默认按照升序排序。

使用场景

对数组进行排序

int arr[] = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
std::sort(arr, arr + sizeof(arr) / sizeof(arr[0]));
// 现在 arr 数组已经按照升序排列为 {1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9}

对 vector 进行排序

std::vector<int> v = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
std::sort(v.begin(), v.end());
// 现在 v 向量已经按照升序排列为 {1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9}

对字符串进行排序

std::string str = "hello world";
std::sort(str.begin(), str.end());
// 现在 str 字符串已经按照字母表顺序排列为 "dehllloorw"

对 pair 进行排序

std::pair<int, std::string> p1 = {3, "apple"};
std::pair<int, std::string> p2 = {1, "banana"};
std::pair<int, std::string> p3 = {2, "orange"};
std::vector<std::pair<int, std::string>> vec = {p1, p2, p3};
std::sort(vec.begin(), vec.end());
// 现在 vec 向量已经按照第一个元素升序排列为 {{1, "banana"}, {2, "orange"}, {3, "apple"}}

完整示例

例子1: 对数组进行正向排序

#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    int arr[] = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
    int n = sizeof(arr) / sizeof(arr[0]);

    sort(arr, arr + n);

    cout << "Sorted array is: ";
    for (int i = 0; i < n; i++) {
        cout << arr[i] << " ";
    }
    cout << endl;

    return 0;
}

输出结果为:Sorted array is: 1 1 2 3 3 4 5 5 5 6 9

例子2: 对 vector 进行正向排序

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    vector<int> v = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};

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

    cout << "Sorted vector is: ";
    for (int i = 0; i < v.size(); i++) {
        cout << v[i] << " ";
    }
    cout << endl;

    return 0;
}

输出结果为:Sorted vector is: 1 1 2 3 3 4 5 5 5 6 9

例子3: 对字符串进行正向排序

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

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

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

    cout << "Sorted string is: " << str << endl;

    return 0;
}

输出结果为:Sorted string is: dehllloorw

例子4: 对 pair 进行正向排序

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

bool compare(const pair<int, string>& a, const pair<int, string>& b) {
    return a.first < b.first;
}

int main() {
    vector<pair<int, string>> v = {{3, "apple"}, {1, "banana"}, {2, "orange"}};

    sort(v.begin(), v.end(), compare);

    cout << "Sorted vector of pairs is: ";
    for (int i = 0; i < v.size(); i++) {
        cout << v[i].first << " " << v[i].second << " ";
    }
    cout << endl;

    return 0;
}

输出结果为:Sorted vector of pairs is: 1 banana 2 orange 3 apple

例子5: 对数组进行反向排序

#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    int arr[] = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
    int n = sizeof(arr) / sizeof(arr[0]);

    sort(arr, arr + n, greater<int>());

    cout << "Reverse sorted array is: ";
    for (int i = 0; i < n; i++) {
        cout << arr[i] << " ";
    }
    cout << endl;

    return 0;
}

输出结果为:Reverse sorted array is: 9 6 5 5 5 4 3 3 2 1 1

例子6: 对 vector 进行反向排序

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    vector<int> v = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};

    sort(v.rbegin(), v.rend(), greater<int>());

    cout << "Reverse sorted vector is: ";
    for (int i = 0; i < v.size(); i++) {
        cout << v[i] << " ";
    }
    cout << endl;

    return 0;
}

输出结果为:Reverse sorted vector is: 9 6 5 5 5 4 3 3 2 1 1

例子7: 对字符串进行反向排序

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

bool compare(const string& a, const string& b) {
    return a > b;
}

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

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

    cout << "Reverse sorted string is: " << str << endl;

    return 0;
}

输出结果为:Reverse sorted string is: dlrow olleh

总结

std::sort函数是C++标准库中常用的排序函数之一,它可以对各种类型的序列进行排序。通过指定不同的比较函数,可以实现不同的排序方式。在实际开发中,我们经常需要对数据进行排序以便进行后续处理或分析。掌握std::sort函数的使用技巧可以帮助我们更高效地完成这些任务。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老狼IT工作室

你的鼓励将是我创作的最大动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值