当前位置: 首页 > news >正文

岳阳网站建设企业足球比赛直播app下载

岳阳网站建设企业,足球比赛直播app下载,tp5网站开发逻辑架构,许昌市网站开发在 C 中&#xff0c;std::vector 可以使用标准库中的 std::sort 函数对元素进行排序。std::sort 位于 <algorithm> 头文件中&#xff0c;是一个非常高效的排序算法&#xff08;通常是快速排序或混合排序&#xff09;。 以下是 std::sort 的基本用法&#xff1a; 1. 包含…

在 C++ 中,std::vector 可以使用标准库中的 std::sort 函数对元素进行排序。std::sort 位于 <algorithm> 头文件中,是一个非常高效的排序算法(通常是快速排序或混合排序)。

以下是 std::sort 的基本用法:


1. 包含头文件

#include <algorithm> // for std::sort
#include <vector>

2. 默认排序(升序)

std::sort 默认使用 < 运算符对元素进行升序排序。

std::vector<int> vec = {5, 2, 9, 1, 5, 6};// 默认升序排序
std::sort(vec.begin(), vec.end());// 输出结果
for (int value : vec) {std::cout << value << " ";
}
// 输出: 1 2 5 5 6 9

3. 降序排序

可以通过传递一个自定义的比较函数来实现降序排序。C++ 提供了 std::greater 来简化这一操作。

#include <functional> // for std::greaterstd::vector<int> vec = {5, 2, 9, 1, 5, 6};// 降序排序
std::sort(vec.begin(), vec.end(), std::greater<int>());// 输出结果
for (int value : vec) {std::cout << value << " ";
}
// 输出: 9 6 5 5 2 1

4. 自定义排序规则

如果需要根据特定规则排序,可以传递一个自定义的比较函数或 lambda 表达式。

示例 1:按绝对值排序
std::vector<int> vec = {-5, 2, -9, 1, 5, 6};// 按绝对值升序排序
std::sort(vec.begin(), vec.end(), [](int a, int b) {return abs(a) < abs(b);
});// 输出结果
for (int value : vec) {std::cout << value << " ";
}
// 输出: 1 2 -5 5 6 -9std::vector<int> vec = {-5, 2, -9, 1, 5, 6};
示例 2:按字符串长度排序
std::vector<std::string> vec = {"apple", "banana", "kiwi", "mango"};// 按字符串长度升序排序
std::sort(vec.begin(), vec.end(), [](const std::string& a, const std::string& b) {return a.length() < b.length();
});// 输出结果
for (const std::string& str : vec) {std::cout << str << " ";
}
// 输出: kiwi mango apple banana

5. 部分排序

如果只需要对部分元素进行排序,可以使用 std::partial_sort

std::vector<int> vec = {5, 2, 9, 1, 5, 6};// 对前 3 个元素进行排序
std::partial_sort(vec.begin(), vec.begin() + 3, vec.end());// 输出结果
for (int value : vec) {std::cout << value << " ";
}
// 输出: 1 2 5 9 5 6

6. 排序自定义对象

如果 std::vector 存储的是自定义对象,可以通过重载 < 运算符或提供自定义比较函数来排序。

示例:按对象的某个成员变量排序
#include <iostream>
#include <vector>
#include <algorithm>struct Person {std::string name;int age;// 重载 < 运算符bool operator<(const Person& other) const {return age < other.age;}
};int main() {std::vector<Person> people = {{"Alice", 25}, {"Bob", 20}, {"Charlie", 30}};// 按年龄升序排序std::sort(people.begin(), people.end());// 输出结果for (const Person& p : people) {std::cout << p.name << " (" << p.age << ")\n";}// 输出:// Bob (20)// Alice (25)// Charlie (30)return 0;
}

7. 性能说明

  • std::sort 的时间复杂度为 O(Nlog⁡N)O(NlogN),其中 NN 是元素的数量。

  • std::sort 是不稳定的排序算法。如果需要稳定排序(保持相等元素的相对顺序),可以使用 std::stable_sort


8. 完整示例

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional> // for std::greaterint main() {std::vector<int> vec = {5, 2, 9, 1, 5, 6};// 默认升序排序std::sort(vec.begin(), vec.end());std::cout << "Ascending: ";for (int value : vec) {std::cout << value << " ";}std::cout << "\n";// 降序排序std::sort(vec.begin(), vec.end(), std::greater<int>());std::cout << "Descending: ";for (int value : vec) {std::cout << value << " ";}std::cout << "\n";return 0;
}

总结

  • std::sort 是 C++ 中最常用的排序函数,适用于 std::vector 和其他容器。

  • 默认是升序排序,可以通过 std::greater 或自定义比较函数实现降序或其他规则排序。

  • 对于自定义对象,可以通过重载 < 运算符或提供比较函数来排序。

http://www.dtcms.com/a/490503.html

相关文章:

  • React 三元运算符页面切换:完整进出流程
  • NumPy zeros_like() 函数详解
  • 网站建设要后台吗公司网页制作哪家好
  • 天津网站建设优化网页设计图片代码
  • CXR SDK实战指南:跨设备AR应用开发
  • 已知明文攻击(Known plaintext):原理、方法与防御体系深度剖析
  • ​SPI四种工作模式
  • 深度学习------YOLOV1和YOLOV2
  • 最小二乘问题详解5:非线性最小二乘求解实例
  • 算法入门数学基础
  • 错误边界:用componentDidCatch筑起React崩溃防火墙
  • 网站备案提交管局原创软文
  • 成都比较好的网站建设公司视频制作和剪辑软件
  • 如何从电脑上卸载安卓应用程序
  • 每日手撕算法--哈希映射/链表存储数求和
  • k8s的pvc和pv
  • RK3562核心板/开发板RT-Linux系统实时性及硬件中断延迟测试
  • node.js把webp,gif格式图片转换成jpg格式图片
  • 不能识别adb/usb口记录
  • SpringBoot-常用注解
  • 支付商城网站制作软件开发报价表
  • wordpress类似的平台快速优化排名公司推荐
  • Git 基础操作指南
  • 网站给部分文字做遮挡代码wordpress主题仿逛丢
  • 【bug】大模型微调bug:OSError: Failed to load tokenizer.| Lora
  • 视频生成的背后机理:Wan2技术报告分析
  • 有什么做衣服的网站吗天津市建筑信息平台
  • HTB BoardLight writeup(enlightenment 0.23.1 exploit)
  • 唐山网站搭建平台制作计划
  • 智能体面试题:ReAct框架 是什么