1-3算法基础-标准模板库STL

1.pair
pair用于存储两个不同类型的值(元素)作为一个单元。它通常用于将两个值捆绑在一起,以便一起传递或返回。

#include <iostream>
#include <utility>
using namespace std;
int main() {pair<int, string> person = make_pair(25, "jack");//存储一对值并初始化
//可简写为  pair<int, string> person(25, "jack");cout << person.first << " " << person.second;//25 jack
}

嵌套

#include <iostream>
#include <utility>
using namespace std;
int main() {pair<int, pair<int, int>> people=make_pair(3, make_pair(4, 5));cout << people.second.first;//4return 0;
}

排序规则
优先考虑first,若相等再比较second…

#include <iostream>
#include <utility>
#include <algorithm>
using namespace std;
int main() {pair<int, std::string> people[] = {make_pair(25, "Alice"),make_pair(30, "Bob"),};sort(people, people + 2);for (int i = 0; i < 2; i++) {cout << people[i].first<< people[i].second << endl;}return 0;
}

2.vector
提供了动态数组(可变大小数组)的实现,存储的事一系列相同类型的元素

#include <iostream>
#include <vector>
using namespace std;
int main() {vector<int> a;//定义整型a容器a.push_back(1);a.push_back(2);a.push_back(3);cout << a[0]<<endl;//首元素:1a.pop_back();//删除尾部元素cout << a.size();//元素个数:2
}

元素插入操作

#include <iostream>
#include <vector>
using namespace std;
int main() {vector<int> a = { 5,4,3 };a.insert(a.begin() + 1, 999);cout << a[1];//999
}

初始化与排序

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {//初始化vector<int> a(5, 1);//对容器a初始化为{1,1,1,1,1}vector<int> b = { 5,4,3,2,1 };//排序sort(b.begin(), b.end());for (int i = 0; i < b.size(); i++) {cout << b[i];//12345}
}

去重排序

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {vector<int> a = { 5,4,3,2,1,5,5 };sort(a.begin(), a.end());//unique只能去掉相邻重复元素,所以先sort//auto用于自动推导 unique 函数返回的迭代器类型auto b = unique(a.begin(), a.end());auto n = distance(a.begin(), b);for (int i = 0; i < n; i++) {cout << a[i];//12345}
}

另一种去重排序方式

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {vector<int> a = { 5,4,3,2,1,5,5 };sort(a.begin(), a.end());//1 2 3 4 5 5 5auto b = unique(a.begin(), a.end());//b指向了多余元素的起始位置(排序完成后的第二个5的位置)a.erase(b, a.end());//擦除从b到末尾的元素,a的大小也同时修改了。即删除了后面的两个5for (int i = 0; i < a.size(); i++) {cout << a[i];//12345}
}

输出可以使用以下方式
循环变量 i 依次取 a 容器中的每个元素的值
const 保证了元素不会被修改
auto 使编译器自动推断元素的类型
& 表示使用引用来避免不必要的复制

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {vector<int> a = { 5,4,3,2,1,5,5 };sort(a.begin(), a.end());auto b = unique(a.begin(), a.end());a.erase(b, a.end());for (const auto& i : a) {cout << i;//12345}
}

3.list
较少使用
以节点存储,以指针链接的链表结构

#include <iostream>
#include <list>
using namespace std;
int main() {// 创建一个空的 list,存储整数list<int> myList;// 在列表末尾插入元素myList.push_back(1);myList.push_back(2);myList.push_back(3);//列表元素为123// 在列表开头插入元素myList.push_front(6);//列表元素为6123// 遍历列表并打印元素for (const auto& i : myList) {cout << i << " ";}cout << endl;//输出了:6 1 2 3// 删除列表中的元素myList.pop_back();myList.pop_front();//列表元素为12
}

在这里插入图片描述
4.stack

#include <iostream>
#include <stack>
using namespace std;
int main() {stack<int> myStack;// 压栈操作myStack.push(1);myStack.push(2);myStack.push(3);//栈内(从下到上):123// 访问栈顶元素cout << myStack.top();//3// 弹栈操作myStack.pop();//3被弹出// 再次访问栈顶元素cout <<  myStack.top();//2//栈内(从下到上):12// 获取栈中元素的数量cout << "Stack size: " << myStack.size() << endl;//2// 检查栈是否为空if (myStack.empty()) {cout << "Stack is empty." << endl;}else {cout << "Stack is not empty." << endl;}
}

在这里插入图片描述
5.queue
(1)普通队列

#include <iostream>
#include <queue>
using namespace std;
int main() {queue<int> myQueue;// 入队操作myQueue.push(1);myQueue.push(2);myQueue.push(3);//队列(从头到尾):123// 访问队头元素cout << myQueue.front();//1// 出队操作myQueue.pop();//1出// 再次访问队头元素cout <<myQueue.front();//2//队列(从头到尾):23// 获取队列中元素的数量cout << myQueue.size();//2// 检查队列是否为空if (myQueue.empty()) {cout << "Queue is empty." << endl;}else {cout << "Queue is not empty." << endl;}
}

(2)优先队列/堆
队列中的元素是按照一定优先级进行排序的,默认情况下是从小到大排序的,即最大元素位于队列的前面。在插入元素时会插入到指定位置,确保队内有序。(大根堆)

在这里插入图片描述

#include <iostream>
#include <queue>
using namespace std;
int main() {priority_queue<int> maxHeap;// 插入元素maxHeap.push(3);maxHeap.push(1);maxHeap.push(4);maxHeap.push(2);while (!maxHeap.empty()) {cout << maxHeap.top()<<" ";maxHeap.pop();}//4 3 2 1
}
#include <iostream>
#include <queue>
using namespace std;
int main() {priority_queue<int> maxHeap;// 插入元素maxHeap.push(3);maxHeap.push(1);maxHeap.push(4);maxHeap.push(2);队列元素:4 3 2 1// 访问队头元素(最大元素)cout << maxHeap.top();//4// 弹出队头元素maxHeap.pop();//4出//队列元素:3 2 1// 再次访问队头元素cout << maxHeap.top();//3// 获取队列中元素的数量cout << maxHeap.size();//3// 检查队列是否为空if (maxHeap.empty()) {cout << "Priority queue is empty." << endl;}else {cout << "Priority queue is not empty." << endl;}
}

小根堆

priority_queue<int, vector<int>, greater<int>> minHeap

(3)双端队列
#include <deque>

#include <iostream>
#include <deque>
using namespace std;
int main() {// 创建一个双端队列deque<int> deque;// 向双端队列的尾部添加元素deque.push_back(1);//队列元素:1deque.push_back(2);//队列元素:1(头) 2(尾)// 向双端队列的头部添加元素deque.push_front(3);//队列元素:3 1 2deque.push_front(4);//队列元素:4 3 1 2// 打印双端队列的元素for (int n : deque) {cout << n;}// 4 3 1 2// 从双端队列的尾部删除元素deque.pop_back();//队列元素:4 3 1// 从双端队列的头部删除元素deque.pop_front();//队列元素:3 1
}

在这里插入图片描述

[例1] CLZ银行问题
在这里插入图片描述
在这里插入图片描述
评测系统

#include <iostream>
#include <string>
#include <queue>
using namespace std;
int main() {int num;cin >> num;//操作数量queue<string> vipQueue;queue<string> normalQueue;for (int i = 0; i < num; ++i) {string operation,name;cin >> operation;if (operation == "IN") {cin >> name;char type;cin >> type;if (type == 'V') {vipQueue.push(name);}else {normalQueue.push(name);}}else {char type;cin >> type;if (type == 'V'&&!vipQueue.empty()) {vipQueue.pop();}else if(type=='N'&&!normalQueue.empty()) {normalQueue.pop();}}}while (!vipQueue.empty()) {cout << vipQueue.front()<<endl;vipQueue.pop();}while (!normalQueue.empty()) {cout << normalQueue.front() << endl;normalQueue.pop();}
}

[例2] 合并果子
在这里插入图片描述
在这里插入图片描述
评测系统

每次选择两个最小的数合并(贪心),并将合并后的新数重新放入堆中

#include <iostream>
#include <string>
#include <queue>
using namespace std;
int main() {int kind;cin >> kind;priority_queue<int, vector<int>, greater<int>> minHeap;for (int i = 0; i < kind; ++i) {int num;cin >> num;minHeap.push(num);}int sum = 0;while (minHeap.size() > 1) {int heapone=minHeap.top();minHeap.pop();int heaptwo = minHeap.top();minHeap.pop();sum += heapone + heaptwo;minHeap.push(heapone + heaptwo);}cout << sum;
}

[例3] 建筑抢修
在这里插入图片描述
在这里插入图片描述

评测系统

我们对建筑按照截止时间进行排序,并使用一个最大堆(优先队列)来动态管理当前的修理计划。每次当总修理时间超过当前考虑的建筑的截止时间时,我们从堆中移除修理时间最长的建筑。

#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
struct building {int repairtime;int deadline;
};
int cmp(building a, building b) {return a.deadline < b.deadline;
}
int main()
{int total = 0;int n;cin >> n;vector<building> a(n);for (int i = 0; i < n; ++i) {cin >> a[i].repairtime >> a[i].deadline;}sort(a.begin(), a.end(), cmp);priority_queue<int> q;for (int i = 0; i < n; i++) {q.push(a[i].repairtime);//一定修,因为马上deadline了total += a[i].repairtime;if (total > a[i].deadline) {//如果超时,那么根据大根堆,队列中维修时长最大的移除total -= q.top();q.pop();}}cout << q.size();return 0;
}

6.set
set存储唯一元素,默认升序

#include <iostream>
#include <set>
using namespace std;int main() {set<int> s;s.insert(3);s.insert(1);s.insert(4);s.insert(2);s.insert(2); // 这个操作没有效果,因为 2 已存在cout << "Set contains:";for (int x : s) {cout << " " << x;}cout << endl;//输出:1 2 3 4
}

降序排列

#include <iostream>
#include <set>
using namespace std;int main() {set<int,greater<int>> s;//改为降序排列s.insert(3);s.insert(1);s.insert(4);s.insert(2);s.insert(2);cout << "Set contains:";for (int x : s) {cout << " " << x;}cout << endl;//输出:4 3 2 1
}

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

multiset允许重复元素

#include <iostream>
#include <set>using namespace std;int main() {multiset<int> ms;ms.insert(3);ms.insert(1);ms.insert(4);ms.insert(2);ms.insert(2);  // 可以插入重复元素// 输出 multiset 的内容cout << "Multiset contains:";for (int x : ms) {cout << " " << x;}//输出:1 2 2 3 4// 计算某个值在 multiset 中出现的次数int count = ms.count(2);cout <<count;//2
}

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

7.map
map存储键值对,键是唯一的,键值对是根据键排序的(自动排序,默认升序排列),可以直接使用键访问对应的值;在插入新元素时,若键已存在,则更新其对应的值

#include <iostream>
#include <map>
using namespace std;int main() {// 创建一个 mapmap<string, int> marks;// 插入键值对marks["Alice"] = 88;marks["Bob"] = 95;marks["Charlie"] = 72;// 更新键对应的值marks["Alice"] = 91;// 遍历并打印 mapfor (const auto& pair : marks) {cout << pair.first << pair.second << endl;}/*输出Alice91Bob95Charlie72*/// 查找并访问元素if (marks.find("Bob") != marks.end()) {cout << marks["Bob"];//95}
}

在这里插入图片描述
8.练习
(1)宝藏排序
在这里插入图片描述
评测系统

#include <iostream>
#include<queue>
using namespace std;
int main() {int n;cin >> n;priority_queue<int,vector<int>,greater<int>> pq;while (n--) {int x;cin >> x;pq.push(x);}while(!pq.empty()) {cout<<pq.top()<<" ";pq.pop();}
}

(2)小蓝吃糖果
在这里插入图片描述
评测系统

找出糖果数量最多的种类,其余糖果进行插空。
若最多种类的糖果数量是max,一共有max-1个空,第二大种类的糖果数一定不超过max-1,越插空越多,以此类推。只有当所有糖果加起来也没有填满max-1个空时,失败。

#include <iostream>
#include <queue>
using namespace std;
int main() {int n;cin >> n;long long int total=0;priority_queue<int> a;for (int i = 0; i < n; i++) {int x;cin >> x;a.push(x);total += x;}int max = a.top();total -= max;if (max - 1 <= total)cout << "Yes";elsecout << "No";
}

(3)小蓝的括号串
在这里插入图片描述
在这里插入图片描述
评测系统

#include <iostream>
#include <stack>
using namespace std;
int main() {stack<char> s;int n;cin >> n;while (n--) {char x;cin >> x;if (x == '(') {s.push(x);}else {if (!s.empty()) {s.pop();}else {cout << "No";return 0;}}}if (!s.empty()) {cout << "No";}else {cout << "Yes";}
}

(4)快递分拣
在这里插入图片描述
在这里插入图片描述
评测系统

#include <iostream>
#include <map>
#include <vector>
using namespace std;
int main() {int n;cin >> n;map<string, vector<string>> m;//一对多的关系,一个键对应多个值vector<string> vm;//记录城市出现顺序while (n--) {string s, p;cin >> s >> p;if (m.find(p) == m.end()) {//当前城市首次出现,可改为if(m.count(p)==0)vm.push_back(p);}m[p].push_back(s);}for (const auto& x : vm) {cout << x <<" " << m[x].size() << endl;for (const auto& x2 : m[x]) {cout << x2 << endl;}}
}

(5)顺子日期
在这里插入图片描述
答案:14

#include<iostream>
#include<string>
using namespace std;
int panduanmonth(int x) {if (x == 1 || x == 3 || x == 5 || x == 7 || x == 8 || x == 10 || x == 12)return 31;else if (x == 4 || x == 6 || x == 9 || x == 11)return 30;else {return 28;}
}
string formatDate(int a, int b, int c) {string s = "2022";if (b < 10) {s+= "0"+to_string(b);}else {s+= to_string(b);}if (c < 10) {s += "0" + to_string(c);}else {s += to_string(c);}return s;
}
bool isSequentialDate(string s) {for (int i = 0; i <= 5; ++i) {if (s[i] + 1 == s[i + 1] && s[i + 1] + 1 == s[i + 2])return true;}return false;
}
int main() {int count = 0;for (int month = 1; month <= 12; month++) {for (int day = 1; day <= panduanmonth(month); day++) {string date = formatDate(2022, month, day);if (isSequentialDate(date)) {count++;}}}cout << count;
}

(6)小明和完美序列
在这里插入图片描述
评测系统
对于序列中的每个数字,计算它出现的次数。比较其值和出现的次数。如果出现次数大于数字的值,则需要删除多余的出现。如果出现次数小于数字的值,则需要全部删除。计算总共需要删除的数字数量。

#include<iostream>
#include<vector>
#include<map>
using namespace std;
int main() {int n;cin >> n;map<int, int> count;int num;for (int i = 0; i < n; ++i) {cin >> num;count[num]++;}int deletecount = 0;for (const auto& x : count) {int a = x.first;int b = x.second;if (a > b) {deletecount += b;}else if (a < b) {deletecount += b - a;}}cout << deletecount;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/212882.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

TailwindCSS 多主题色配置

TailwindCSS 多主题色配置 现在大多数网站都支持主题色变换&#xff0c;比如切换深色模式。那么我们该如何进行主题色配置呢&#xff1f; tailwind dark tailwind 包含一个 dark变体&#xff0c;当启用深色模式时&#xff0c;可以为网站设置不同样式 <div class"bg-whi…

ThingWorx 9.2 Windows安装

参考官方文档安装配置 1 PostgreSQL 13.X 2 Java, Apache Tomcat, and ThingWorx PTC Help Center 参考这里安装 数据库 C:\ThingworxPostgresqlStorage 设置为任何人可以full control 数据库初始化 pgadmin4 创建用户twadmin并记录口令password Admin Userpostgres Thin…

漏刻有时百度地图API实战开发(9)Echarts使用bmap.js实现轨迹动画效果

Bmap.js是Echarts和百度地图相结合开发的一款JavaScript API&#xff0c;它可以帮助用户在web应用中获取包括地图中心点、地图缩放级别、地图当前视野范围、地图上标注点等在内的地图信息&#xff0c;并且支持在地图上添加控件&#xff0c;提供包括智能路线规划、智能导航(驾车…

C# WPF上位机开发(通讯协议的编写)

【 声明&#xff1a;版权所有&#xff0c;欢迎转载&#xff0c;请勿用于商业用途。 联系信箱&#xff1a;feixiaoxing 163.com】 作为上位机&#xff0c;它很重要的一个部分就是需要和外面的设备进行数据沟通的。很多时候&#xff0c;也就是在这个沟通的过程当中&#xff0c;上…

PyQt下使用OpenCV实现人脸检测与识别

背景&#xff1a; 一 数字图像处理与识别警务应用模型 基于前期所学知识&#xff0c;与公安实践相结合&#xff0c;综合设计数字图像处理与识别警务应用模型,从下列4个研究课题中选择2个进行实验实现&#xff1a;图像增强与复原、人脸检测与识别、虹膜内外圆检测与分割、车牌…

Html转PDF,前端JS实现Html页面导出PDF(html2canvas+jspdf)

Html转PDF&#xff0c;前端JS实现Html页面导出PDF&#xff08;html2canvasjspdf&#xff09; 文章目录 Html转PDF&#xff0c;前端JS实现Html页面导出PDF&#xff08;html2canvasjspdf&#xff09;一、背景介绍二、疑问三、所使用技术html2canvasjspdf 四、展示开始1、效果展示…

C语言----文件操作(一)

一&#xff1a;C语言中文件的概念 对于文件想必大家都很熟悉&#xff0c;无论在windows上还是Linux中&#xff0c;我们用文件去存储资料&#xff0c;记录笔记&#xff0c;常见的如txt文件&#xff0c;word文档&#xff0c;log文件等。那么&#xff0c;在C语言中文件是什么样的存…

threadpool github线程池学习

参考项目 https://github.com/progschj/ThreadPool 源码分析 // 常规头文件保护宏, 避免重复 include #ifndef THREAD_POOL_H #define THREAD_POOL_H// 线程池, 存储线程对象; #include <vector>// 任务队列, 双向都可操作队列, queue 不能删除首个元素 #include <…

微信小程序制作-背单词的小程序制作

微信小程序–背单词的 好久没有发过文章了&#xff0c;但是不代表着我不去学习了喽&#xff0c;以下是我最近做的东西&#xff0c;前端的UI由朋友设计的&#xff0c;目前这个是前端使用的是微信小程序后端是Python的一个轻量型框架&#xff0c;FastApi&#xff0c;嗯&#xff…

MyBatis 四大核心组件之 Executor 源码解析

&#x1f680; 作者主页&#xff1a; 有来技术 &#x1f525; 开源项目&#xff1a; youlai-mall &#x1f343; vue3-element-admin &#x1f343; youlai-boot &#x1f33a; 仓库主页&#xff1a; Gitee &#x1f4ab; Github &#x1f4ab; GitCode &#x1f496; 欢迎点赞…

List 接口

1 List 接口 java.util 中的集合类包含 Java 中某些最常用的类。最常用的集合类是 List 和 Map。 List是一种常用的集合类型&#xff0c;它可以存储任意类型的对象&#xff0c;也可以结合泛型来存储具体的类型对象&#xff0c;本质上就是一个容器。 1.1 List 类型介绍 有序性…

06-React组件 Redux React-Redux

React组件化&#xff08;以Ant-Design为例&#xff09; 组件化编程&#xff0c;只需要去安装好对应的组件&#xff0c;然后通过各式各样的组件引入&#xff0c;实现快速开发 我们这里学习的是 Ant-design &#xff08;应该是这样&#xff09;&#xff0c;它有很多的组件供我们…

计算机网络测试题第二部分

前言:如果没有做在线测试请自主独立完成&#xff0c;本篇文章只作为学习计算机网络的参考&#xff0c;题库中的题存在一定错误和不完整&#xff0c;请学习时&#xff0c;查找多方书籍论证&#xff0c;独立思考&#xff0c;如果存在疑虑可以评论区讨论。查看时&#xff0c;请分清…

pytorch debug 常用工具

自动辨识图像格式可视化 import numpy as np import matplotlib.pyplot as plt from PIL import Imagedef convert_to_numpy(image_input):"""自动检测输入图像类型&#xff0c;并将其转换为NumPy数组。"""if isinstance(image_input, np.ndarr…

7-3 Left-pad

根据新浪微博上的消息&#xff0c;有一位开发者不满NPM&#xff08;Node Package Manager&#xff09;的做法&#xff0c;收回了自己的开源代码&#xff0c;其中包括一个叫left-pad的模块&#xff0c;就是这个模块把javascript里面的React/Babel干瘫痪了。这是个什么样的模块&a…

物联网IC

物联网IC 电子元器件百科 文章目录 物联网IC前言一、物联网IC是什么二、物联网IC的类别三、物联网IC的应用实例四、物联网IC的作用原理总结前言 物联网IC的功能和特性可以根据不同的物联网应用需求来选择和配置,以满足物联网设备在连接、通信、感知和控制方面的需求。 一、物…

猜数字游戏Ⅱ

你和朋友一起玩猜数字游戏&#xff0c;你写出一个秘密数字&#xff0c;请朋友猜这个数字是多少。朋友每猜测一次&#xff0c;你就会给他一个包含下述信息的提示&#xff1a; 猜测数字中有多少位属于数字和确切位置都猜对了&#xff08;称为 "Bulls"&#xff0c;公牛&…

VOL-vue 框架 文件上传控件关于大文件上传等待的修改

我的项目在测试voltable列表组件中对阿里云OSS做附件上传时&#xff0c;几十M的文件可能就会需要一段时间来上传&#xff0c;才能有OSS的状态和链接返回。 但是控件VolUpload.vue并没有去在这方面做任何交互体验上的控制&#xff0c;而且VolUpload.vue本身写的几个上传函数都是…

SpringBoo在项目停止(服务停止/关闭退出)之后执行的方法

SpringBoo在项目停止/服务停止/关闭退出之后执行的方法 1.实现DisposableBean接口2.使用PreDestroy注解 SpringApplication会向JVM注册一个关闭钩子(hook)&#xff0c;以确保ApplicationContext在退出时正常关闭。 可以使用所有标准的Spring生命周期回调&#xff08;例如Dispos…

内测分发是什么?十年的前端开发者带你了解

内测分发是软件开发过程中的一个阶段&#xff0c;特别指软件还未完全完成或准备对外广泛发布前&#xff0c;向一定范围的用户群体提供该软件版本的测试机会&#xff0c;以便收集反馈和修复潜在的问题。在讲解内测分发之前&#xff0c;我们需要明确几个相关概念&#xff1a; 软件…