Android常用C++特性之std::sort

news/2024/9/28 7:03:01 标签: c++

声明:本文内容生成自ChatGPT,目的是为方便大家了解学习作为引用到作者的其他文章中。

std::sort 是 C++ 标准库中的一个函数,用于对容器或数组中的元素进行排序。它是一个非常高效的排序算法,通常使用快速排序或混合排序(如 intro sort)实现,具体的实现取决于编译器。

语法

#include <algorithm>

template <class RandomIt>
void sort(RandomIt first, RandomIt last);

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

参数

  • first, last:定义要排序范围的迭代器。排序是左闭右开的,即 [first, last)
  • comp(可选):一个自定义的比较函数或对象,用于定义排序规则。

返回值

  • std::sort 没有返回值,但它会对给定范围的元素进行原地排序。

示例

1. 默认排序(升序)
#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> vec = {5, 2, 9, 1, 5, 6};

    // 使用默认的升序排序
    std::sort(vec.begin(), vec.end());

    std::cout << "Sorted vector: ";
    for (const auto& num : vec) {
        std::cout << num << " ";
    }
    std::cout << std::endl;

    return 0;
}

输出:

Sorted vector: 1 2 5 5 6 9
2. 自定义排序(降序)
#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> vec = {5, 2, 9, 1, 5, 6};

    // 使用自定义的降序排序
    std::sort(vec.begin(), vec.end(), std::greater<int>());

    std::cout << "Sorted vector (descending): ";
    for (const auto& num : vec) {
        std::cout << num << " ";
    }
    std::cout << std::endl;

    return 0;
}

输出:

Sorted vector (descending): 9 6 5 5 2 1
3. 自定义比较函数

你可以提供自己的比较函数或 Lambda 表达式,用于定义排序规则。

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

bool customCompare(int a, int b) {
    return (a % 10) < (b % 10);  // 比较个位数
}

int main() {
    std::vector<int> vec = {15, 32, 23, 74, 56, 92};

    // 使用自定义的比较函数
    std::sort(vec.begin(), vec.end(), customCompare);

    std::cout << "Sorted vector by last digit: ";
    for (const auto& num : vec) {
        std::cout << num << " ";
    }
    std::cout << std::endl;

    return 0;
}

输出:

Sorted vector by last digit: 32 92 23 74 15 56

4. 排序结构体或类的对象

你可以使用 std::sort 对自定义对象进行排序,并通过自定义比较函数来指定排序条件。

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

struct Person {
    std::string name;
    int age;
};

// 比较函数:按年龄排序
bool compareByAge(const Person& a, const Person& b) {
    return a.age < b.age;
}

int main() {
    std::vector<Person> people = {{"Alice", 30}, {"Bob", 25}, {"Charlie", 35}};

    // 按年龄升序排序
    std::sort(people.begin(), people.end(), compareByAge);

    std::cout << "People sorted by age: " << std::endl;
    for (const auto& person : people) {
        std::cout << person.name << " (" << person.age << ")" << std::endl;
    }

    return 0;
}

输出:

People sorted by age: 
Bob (25)
Alice (30)
Charlie (35)

5. 排序部分范围

你可以对一个容器或数组的部分范围进行排序。

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

int main() {
    std::vector<int> vec = {5, 2, 9, 1, 5, 6};

    // 只对部分元素进行排序,从第二个到第四个元素(索引从0开始)
    std::sort(vec.begin() + 1, vec.begin() + 4);

    std::cout << "Partially sorted vector: ";
    for (const auto& num : vec) {
        std::cout << num << " ";
    }
    std::cout << std::endl;

    return 0;
}

输出:

Partially sorted vector: 5 1 2 9 5 6

注意事项

  • std::sort 只适用于随机访问迭代器(如数组、std::vector)。它不适用于链表(如 std::list),因为链表的迭代器不支持随机访问。
  • std::sort 的时间复杂度为 O(N log N),其中 N 是待排序元素的数量。

总结

  • std::sort 是 C++ 中的高效排序函数,默认使用快速排序或类似算法。
  • 支持默认升序排序和自定义比较规则(如降序、自定义函数等)。
  • 适用于随机访问迭代器,如数组和 std::vector 等容器。

http://www.niftyadmin.cn/n/5680611.html

相关文章

小阿轩yx-案例:Ansible剧本文件实践

小阿轩yx-案例&#xff1a;Ansible剧本文件实践 Playbook 介绍 什么是 playbook playbook 顾名思义&#xff0c;即剧本&#xff0c;现实生活中演员按照剧本表演在 ansible 中&#xff0c;由被控计算机表演&#xff0c;进行安装&#xff0c;部署应用&#xff0c;提供对外的服…

WebAssembly与WebGPU:游戏开发的新时代

文章目录 WebAssembly简介WebGPU简介Wasm WebGPU 在游戏开发中的优势创建一个简单的WebAssembly模块使用WebGPU绘制一个三角形WebAssembly 的高级特性内存管理异步加载与多线程 WebGPU 的高级特性着色器编程计算着色器 实战案例&#xff1a;创建一个简单的 2D 游戏游戏逻辑设计…

无人机之虚拟云台技术篇

一、概念解释 虚拟云台技术&#xff0c;并非直接安装在无人机上的机械装置&#xff0c;而是通过软件算法和传感器技术&#xff0c;模拟出物理云台的功能&#xff0c;实现对相机或传感器的稳定控制。这种技术通过高精度的算法和实时数据处理&#xff0c;能够在无人机飞行过程中&…

C# + SQLiteExpert 进行(cipher)加密数据库开发+Costura.Fody 清爽发布

一&#xff1a;让 SQLiteExpert 支持&#xff08;cipher&#xff09;加密数据库 SQLiteExpert 作为SQlite 的管理工具&#xff0c;默认不支持加密数据库的&#xff0c;使其成为支持&#xff08;cipher&#xff09;加密数据库的管理工具&#xff0c;需要添加e_sqlcipher.dll &…

RabbitMQ 实验入门

使用 spring-amqp 实验 发布订阅模型 fanoutExchange 实验 实验步骤&#xff1a; 编写定义 队列 和 交换机 绑定关系的代码创建接口&#xff0c;模拟生产者&#xff0c;方便调试&#xff08;接受参数 队列名、路由键、[消息]&#xff09;定义消费者 代码示例&#xff1a; C…

yolo自动化项目实例解析(七)自建UI--工具栏选项

在上一章我们基本实现了关于预览窗口的显示&#xff0c;现在我们主要完善一下工具栏菜单按键 一、添加工具栏ui 1、配置文件读取 我们后面要改的东西越来越多了&#xff0c;先加个变量文件方便我们后面调用 下面我们使用的config.get意思是从./datas/setting.ini文件中读取关键…

Python编码系列—Python责任链模式:打造灵活的请求处理流程

&#x1f31f;&#x1f31f; 欢迎来到我的技术小筑&#xff0c;一个专为技术探索者打造的交流空间。在这里&#xff0c;我们不仅分享代码的智慧&#xff0c;还探讨技术的深度与广度。无论您是资深开发者还是技术新手&#xff0c;这里都有一片属于您的天空。让我们在知识的海洋中…

什么是原生IP?

代理IP的各个类型称呼有很多&#xff0c;且它们在网络使用和隐私保护方面扮演着不同的角色。今天将探讨什么是原生IP以及原生IP和住宅IP之间的区别&#xff0c;帮助大家更好地理解这两者的概念和实际应用&#xff0c;并选择适合自己的IP类型。 一、什么是原生IP&#xff1f; 原…