C++——STL 常用的排序算法

devtools/2025/3/17 4:46:55/

算法简介:

  • sort       // 对容器内元素进行排序
  • reandom_shuffle    // 洗牌 指定范围内的元素随机调整次序
  • merge     // 容器元素合并,并存储到另一个容器中
  • reverse     // 反转指定范围的元素

1. sort

  • 功能描述:
    • 对容器内元素进行排序
  • 函数原型:
sort(iterator beg, iterator end, _Perd);

        // beg 开始迭代器

        // end 结束迭代器    

        // _Pred 谓词 

  • 示例:
#include<iostream>
using namespace std;
#include <vector>
#include <algorithm>
#include <string>
#include <functional>// 常用排序算法 sort
void myPrint(int val){cout << val << " ";
}void test01(){vector<int>v;v.push_back(10);v.push_back(30);v.push_back(50);v.push_back(20);v.push_back(40);// 利用sort进行升序sort(v.begin(),v.end());for_each(v.begin(),v.end(),myPrint);cout << endl;// 降序sort(v.begin(),v.end(),greater<int>());for_each(v.begin(),v.end(),myPrint);cout << endl;
}int main(){test01();return 0;
}
// 10 20 30 40 50
// 50 40 30 20 10

2. random_shuffle

  • 功能描述
    • 洗牌:指定范围内的元素随机调整次序
  • 函数原型:
random_shuffle(iterator beg, iterator end);

        //beg 开始迭代器

        //end 结束迭代器 

  • 示例:
#include<iostream>
using namespace std;
#include <vector>
#include <algorithm>
#include <string>
#include <functional>
#include <ctime>
// 常用排序算法 random_shuffle
void myPrint(int val){cout << val << " ";
}
void test01(){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(),myPrint);cout << endl;
}int main(){srand((unsigned int)time(NULL));test01();return 0;
}

3. mrege

  • 功能描述:
    • 两个容器合并,并存储到另一个容器中
  • 函数原型:
merge(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);

        // 注意:两个容器必须是有序的

        // beg1 容器1开始迭代器

        // end1 容器1结束迭代器

        // beg2 容器2开始迭代器

        // end2 容器2结束迭代器

        // dest 目标容器开始迭代器 

  • 示例:
#include<iostream>
using namespace std;
#include <vector>
#include <algorithm>
#include <string>
#include <functional>
// 常用排序算法 merge
void myPrint(int val){cout << val << " ";
}
void test01(){vector<int>v1;vector<int>v2;for (int i = 0; i < 10; i++){v1.push_back(i);v2.push_back(i+1);}vector<int>vTarget;vTarget.resize(v1.size() + v2.size());merge(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());for_each(vTarget.begin(),vTarget.end(),myPrint);cout << endl;
}int main(){test01();return 0;
}
//0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10

 4. reverse

  • 功能描述:
    • 将容器内元素进行反转
  • 函数原型:
reverse(ierator beg, iterator end);

        // beg 开始迭代器

        // end 结束迭代器 

  • 示例:
#include<iostream>
using namespace std;
#include <vector>
#include <algorithm>
#include <string>
#include <functional>
// 常用排序算法 reverse
class myPrint
{
public:void operator()(int val){cout << val << " ";}
};
void test01(){vector<int>v;v.push_back(10);v.push_back(30);v.push_back(50);v.push_back(20);v.push_back(40);cout << "反转前:" << endl;for_each(v.begin(),v.end(),myPrint());cout << endl;cout << "反转后" << endl;reverse(v.begin(), v.end());for_each(v.begin(),v.end(),myPrint());cout << endl;
}
int main(){test01();return 0;
}
// 反转前:
// 10 30 50 20 40
// 反转后:
// 40 20 50 30 10


http://www.ppmy.cn/devtools/167734.html

相关文章

卸载Linux自带的MariaDB操作过程

安装及配置 1、下载安装包mysql-version-linux-glibc2.5-x86_64.tar&#xff08;可前往官网自行下载&#xff1a;http://dev.mysql.com/downloads/mysql/&#xff09; 2、卸载系统自带的MariaDB 打开Terminal终端&#xff1a; # rpm -qa|grep mariadb //查询出来已安装的ma…

PyTorch 深度学习实战(14):Deep Deterministic Policy Gradient (DDPG) 算法

在上一篇文章中&#xff0c;我们介绍了 Proximal Policy Optimization (PPO) 算法&#xff0c;并使用它解决了 CartPole 问题。本文将深入探讨 Deep Deterministic Policy Gradient (DDPG) 算法&#xff0c;这是一种用于连续动作空间的强化学习算法。我们将使用 PyTorch 实现 D…

条件运算符

在 MySQL 中&#xff0c;BETWEEN...AND 语句是一个用于筛选数据的条件运算符&#xff0c;它可以帮助你从表中选取指定范围内的数据。 SELECT column1, column2, ... FROM table_name WHERE column_name BETWEEN value1 AND value2; 如果你想选取不在指定范围内的数据&#xff0…

将景区天气数据存储到Excel文件中

笔记 import weather import openpyxl htmlweather.get_html() # 发请求&#xff0c;得响应结果 lstweather.parse_html(html) # 解析数据 # 创建一个新的Excel工作簿 workbookopenpyxl.Workbook() # 创建对象 # 在Excel文件中创建工作表 sheetworkbook.create_sheet(景区天气…

DeepSeek-prompt指令-当DeepSeek答非所问,应该如何准确的表达我们的诉求?

当DeepSeek答非所问&#xff0c;应该如何准确的表达我们的诉求&#xff1f;不同使用场景如何向DeepSeek发问&#xff1f;是否有指令公式&#xff1f; 目录 1、 扮演专家型指令2、 知识蒸馏型指令3、 颗粒度调节型指令4、 时间轴推演型指令5、 极端测试型6、 逆向思维型指令7、…

多级缓存架构实战:Caffeine+Redis

一、架构演进与核心价值 1.1 性能对比数据 1.2 协同设计优势 二、实战案例&#xff1a;电商商品详情页优化 2.1 痛点分析 原始架构&#xff1a;单层 Redis 缓存 问题现象&#xff1a; # 压测数据 Requests/sec: 58000 99% latency: 120ms Redis CPU Usage: 85%2.2 架构改…

机器学习——正则化、欠拟合、过拟合、学习曲线

过拟合&#xff08;overfitting&#xff09;:模型只能拟合训练数据的状态。即过度训练。 避免过拟合的几种方法&#xff1a; ①增加全部训练数据的数量&#xff08;最为有效的方式&#xff09; ②使用简单的模型&#xff08;简单的模型学不够&#xff0c;复杂的模型学的太多&am…

[人工智能]实现神经网络实例

import numpy as np&#xff1a;导入 NumPy 库&#xff0c;用于数值计算。导入 PyTorch 相关库&#xff1a; import torch&#xff1a;导入 PyTorch 库&#xff0c;深度学习框架核心库。from torchvision.datasets import mnist&#xff1a;从torchvision.datasets中导入 MNIST…