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

C++之常用的排序算法

C++之常用的排序算法

在这里插入图片描述

sort

在这里插入图片描述

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<functional>
void Myptint(int val)
{cout << val << " ";
}void test()
{vector<int> v;v.push_back(10);v.push_back(20);v.push_back(50);v.push_back(30);v.push_back(40);//利用sort进行排序(默认是升序)sort(v.begin(), v.end());for_each(v.begin(),v.end(), Myptint);cout << endl;//改变为降序sort(v.begin(), v.end(), greater<int>());for_each(v.begin(), v.end(), Myptint);cout << endl;
}int main()
{test();system("pause");return 0;
}

在这里插入图片描述

random_shuffle

在这里插入图片描述

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<ctime>
void Myptint(int val)
{cout << val << " ";
}void test()
{//随机种子srand((unsigned int)time(NULL));vector<int> v;for (int i = 0; i < 10; i++){v.push_back(i);}random_shuffle(v.begin(), v.end());for_each(v.begin(), v.end(), Myptint);cout << endl;
}int main()
{test();system("pause");return 0;
}

在这里插入图片描述

merge

在这里插入图片描述

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>void Myptint(int val)
{cout << val << " ";
}void test()
{vector<int> v;vector<int>v2;for (int i = 0; i < 10; i++){v.push_back(i);v2.push_back(i+1);}//目标容器vector<int>Target;//提前给目标容器分配空间Target.resize(v.size()+v2.size());merge(v.begin(), v.end(), v2.begin(), v2.end(), Target.begin());for_each(Target.begin(), Target.end(), Myptint);cout << endl;
}int main()
{test();system("pause");return 0;
}

在这里插入图片描述
在这里插入图片描述

reverse

在这里插入图片描述

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>void Myptint(int val)
{cout << val << " ";
}void test()
{vector<int> v;for (int i = 0; i < 10; i++){v.push_back(i);}//反转前cout << "反转前" << endl;for_each(v.begin(), v.end(), Myptint);cout << endl;cout << "反转后" << endl;reverse(v.begin(), v.end());for_each(v.begin(), v.end(), Myptint);cout << endl;
}int main()
{test();system("pause");return 0;
}

在这里插入图片描述

http://www.lryc.cn/news/239424.html

相关文章:

  • Mac中LaTex无法编译的问题
  • 【Python爬虫】8大模块md文档集合从0到scrapy高手,第7篇:selenium 数据提取详解
  • 【python基础(三)】操作列表:for循环、正确缩进、切片的使用、元组
  • 使用VSCode调试全志R128的C906 RISC-V核心
  • Node.js之http模块
  • golang 断点调试
  • 定时器如何计算触发频率?
  • 【数据库】数据库中的检查点Checkpoint,数据落盘的重要时刻
  • 关于 Docker
  • ​LeetCode解法汇总2342. 数位和相等数对的最大和
  • 数据库的级联删除
  • 【Python 千题 —— 基础篇】奇数列表
  • 当npm下载库失败时可以用cnpm替代
  • PyTorch多GPU训练时同步梯度是mean还是sum?
  • Spring Framework IoC依赖注入-按Bean类型注入
  • IDEA运行thymeleaf的html文件打开端口为63342且连不上数据库
  • sql报错注入和联合注入
  • 028 - STM32学习笔记 - ADC结构体学习(二)
  • Pytest自动化测试框架:mark用法---测试用例分组执行
  • 【TCP连接的状态】
  • Node.js入门指南(一)
  • 使用Grpc实现高性能PHP RPC服务
  • 二、爬虫-爬取肯德基在北京的店铺地址
  • linux驱动开发.之spi测试工具spidev_test源码(一)
  • 基于材料生成算法优化概率神经网络PNN的分类预测 - 附代码
  • Go——二、变量和数据类型
  • 合并区间问题
  • 2023 年最新 MySQL 数据库 Windows 本地安装、Centos 服务器安装详细教程
  • 每天一道算法题(十)——获取和为k的子数组
  • 2023年亚太杯数学建模思路 - 案例:最短时间生产计划安排