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

14 C++ STL 容器实战:stack/list 模拟实现指南 + priority_queue 用法及避坑技巧

 stack和queue

stack的模拟实现和应用--底层就是顺序表

从栈的接口中可以看出,栈实际是一种特殊的vector,因此使用vector完全可以模拟实现stack。

#include<vector>
namespace Stack
{
template<class T>
class stack
{
public:stack() {}void push(const T& x) {_c.push_back(x);}void pop() {_c.pop_back();}T& top() {return _c.back();}const T& top()const {return _c.back();}size_t size()const {return _c.size();}bool empty()const {return _c.empty();}private:std::vector<T> _c;
};
}
应用:逆波兰表达式求一个混合表达式的计算结果

queue的介绍和使用

queue的模拟实现 因为queue的接口中存在头删和尾插,因此使用vector来封装效率太低,故可以借助list来模拟实 现queue,具体如下:

#include <list>
namespace List   
{
template<class T>
class queue
{
public:queue() {}void push(const T& x) {_c.push_back(x);}void pop() {_c.pop_front();}T& back() {return _c.back();}const T& back()const {return _c.back();}T& front() {return _c.front();}const T& front()const {return _c.front();}size_t size()const {return _c.size();}bool empty()const {return _c.empty();}private:std::list<T> _c;
};

priority_queue的介绍和使用--底层是堆结构(完全二叉搜索树)

需要支持随机访问迭代器,以便始终在内部保持堆结构。容器适配器通过在需要时自动调用算法函数make_heap、push_heap和pop_heap来自动完成此操作。

优先级队列默认使用vector作为其底层存储数据的容器,在vector上又使用了堆算法将vector中元素构造成堆的结构,因此priority_queue就是堆,所有需要用到堆的位置,都可以考虑使用priority_queue。注意:默认情况下priority_queue是小堆(堆顶元素是最小的)

#include <vector>
#include <queue>
#include <functional>  // greater算法的头文件
void TestPriorityQueue()
{
// 默认情况下,创建的是小堆,其底层按照小于号比较
//如果左右子节点的最小值小于父亲节点就交换
vector<int> v{3,2,7,6,0,4,1,9,8,5};
priority_queue<int> q1;//不指定底层容器默认就是vector
for (auto& e : v)
q1.push(e);
cout << q1.top() << endl;
// 如果要创建大堆,将第三个模板参数换成greater比较方式,指定底层容器是vector//左右子节点的最大值如果大于父亲节点就交换
priority_queue<int, vector<int>, greater<int>> q2(v.begin(), v.end());
cout << q2.top() << endl;
}
小堆应用--前k大的元素

依次入k+1个元素进堆,堆顶的元素就是最小的,小于堆里面的k个元素,弹出堆顶元素,继续入下一个元素,此时堆里的k+1个元素的最小的又被推到堆顶,弹出即可,循环操作即可。

堆的遍历输出--是排序好的

小堆遍历输出每个元素是降序的,大堆是升序的

void TestPriorityQueue()
{// 默认情况下,创建的是小堆,其底层按照大于号比较vector<int> v{ 3,2,7,6,0,4,1,9,8,5 };priority_queue<int> q1;for (auto& e : v)q1.push(e);while(!q1.empty())//小堆降序{cout << q1.top();//9876543210q1.pop();}cout << endl;// 如果要创建大堆,将第三个模板参数换成greater比较方式priority_queue<int, vector<int>, greater<int>> q2(v.begin(), v.end());while (!q2.empty())//大堆遍历升序{cout << q2.top();//0123456789q2.pop();}cout << endl;
}
使用注意点--存储自定义类型的对象

如果在priority_queue中放自定义类型的数据,用户需要在自定义类型中提供> 或者< 的重 载

lass Date
{public:
Date(int year = 1900, int month = 1, int day = 1)
: _year(year)
, _month(month)
, _day(day)
{}
bool operator<(const Date& d)const
{
return (_year < d._year) ||(_year == d._year && _month < d._month) ||(_year == d._year && _month == d._month && _day < d._day);
}
bool operator>(const Date& d)const
{
return (_year > d._year) ||
(_year == d._year && _month > d._month) ||
(_year == d._year && _month == d._month && _day > d._day);
}
friend ostream& operator<<(ostream& _cout, const Date& d)
{
_cout << d._year << "-" << d._month << "-" << d._day;
return _cout;
}private:
int _year;
int _month;
int _day;
};void TestPriorityQueue()
{
// 大堆,需要用户在自定义类型中提供<的重载
priority_queue<Date> q1;
q1.push(Date(2018, 10, 29));
q1.push(Date(2018, 10, 28));
q1.push(Date(2018, 10, 30));
cout << q1.top() << endl;// 如果要创建小堆,需要用户提供>的重载
priority_queue<Date, vector<Date>, greater<Date>> q2;
q2.push(Date(2018, 10, 29));
q2.push(Date(2018, 10, 28));
q2.push(Date(2018, 10, 30));cout << q2.top() << endl;
}


文章转载自:

http://8GNnk5iV.kxsnp.cn
http://6pj3te62.kxsnp.cn
http://3z1uJ2w5.kxsnp.cn
http://Groc76yd.kxsnp.cn
http://hXDWhUhD.kxsnp.cn
http://T35AqV87.kxsnp.cn
http://HnSzSLav.kxsnp.cn
http://qKaKlnSM.kxsnp.cn
http://vu1OyzQI.kxsnp.cn
http://wgI9mrre.kxsnp.cn
http://qthJ7VP1.kxsnp.cn
http://CJXOk5NE.kxsnp.cn
http://JZZBI5Oa.kxsnp.cn
http://orJp3w0v.kxsnp.cn
http://8eSWxalH.kxsnp.cn
http://quDq4nSW.kxsnp.cn
http://yOVg7UVO.kxsnp.cn
http://aT3scw5V.kxsnp.cn
http://8MemhgzV.kxsnp.cn
http://t2PfvPRC.kxsnp.cn
http://te8nDSF4.kxsnp.cn
http://ZwYDqxes.kxsnp.cn
http://gaJYW38b.kxsnp.cn
http://gfmuyPd5.kxsnp.cn
http://tIGu2Ecf.kxsnp.cn
http://lDBoKEWQ.kxsnp.cn
http://lSexiGXb.kxsnp.cn
http://ecA5fEhp.kxsnp.cn
http://d9uex8re.kxsnp.cn
http://FqlrWivg.kxsnp.cn
http://www.dtcms.com/a/368979.html

相关文章:

  • ElasticSearch新角色的创建及新用户的创建
  • 【运维】Linux inotify watches 限制问题解决方案
  • ES模块(ESM)、CommonJS(CJS)和UMD三种格式
  • centos下gdb调试python的core文件
  • 计算机网络2 第二章 物理层——用什么方式传输邮件
  • 使用深度Q网络(DQN)算法实现游戏AI
  • 深度学习优化框架(DeepSpeed)
  • Java 8 终于要被淘汰了!带你速通 Java 8~24 新特性 | 又能跟面试官吹牛皮了
  • 操作系统重点
  • 安全运维-云计算系统安全
  • HTML 各种标签的使用说明书
  • BYOFF (Bring Your Own Formatting Function)解析(80)
  • MySQL源码部署(rhel7)
  • HashMap多线程下的循环链表问题
  • 企业微信AI怎么用?食品集团靠它砍掉50%低效操作,答案就是选对企业微信服务商
  • 企业微信AI怎么用才高效?3大功能+5个实操场景,实测效率提升50%
  • Arduino Nano33 BLESense Rev2【室内空气质量检测语音识别蓝牙调光台灯】
  • 无人机小目标检测新SOTA:MASF-YOLO重磅开源,多模块协同助力精度飞跃
  • 本地 Docker 环境 Solr 配置 SSL 证书
  • SQL中TRUNCATE vs. DELETE 命令对比
  • RequestContextFilter介绍
  • [密码学实战](GBT 15843.3)基于SM2数字签名的实体鉴别实现完整源码(四十九)
  • 【UE】 实现指向性菲涅尔 常用于圆柱体的特殊菲涅尔
  • 标签系统的架构设计与实现
  • 卫星在轨光压计算详解
  • 摄像头模块的种类:按结构分类
  • 第8篇:决策树与随机森林:从零实现到调参实战
  • 迁移学习-ResNet
  • CentOS安装或升级protoc
  • 【QT 5.12.12 下载 Windows 版本】