news 2026/5/1 9:02:27

stack queue

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
stack queue

模拟实现:

namespace bit { #include<deque> template<class T, class Con = deque<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: Con _c; }; template<class T, class Con = deque<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: Con _c; }; };

priority_queue:

namespace bit { //函数对象 less template<class T> struct less { bool operator()(const T& left, const T& right) { return left < right; } }; //函数对象 greater template<class T> struct greater { bool operator()(const T& left, const T& right) { return left > right; } }; namespace bit { //函数对象 less template<class T> struct less { bool operator()(const T& left, const T& right) { return left < right; } }; //函数对象 greater template<class T> struct greater { bool operator()(const T& left, const T& right) { return left > right; } }; template <class T, class Container = std::vector<T>, class Compare = less<T> > class priority_queue { public: //创建空的优先级队列 priority_queue():c() {} template <class InputIterator> priority_queue(InputIterator first, InputIterator last) :c(first, last) { //将c中的元素调整为堆的结构,默认为大堆 int count = c.size(); int root = ((count - 2) >> 1); for (; root >= 0; root--) AdjustDown(root); } bool empty() const { return c.empty(); } size_t size() const { return c.size(); } // 堆顶元素不允许修改,因为:堆顶元素修改可以会破坏堆的特性 const T& top() const { return c.front(); } void push(const T& x) { c.push_back(x); //push_heap(c.begin(), c.end(), comp); AdjustUp(c.size() - 1); } void pop() { if (empty()) return; std:swap(c.front(), c.back()); c.pop_back(); AdjustDown(0); } private: //向上调整 void AdjustUp(int child) { int parent = ((child - 1) >> 1); while (child) { if (Com(c[parent], c[child])) { std::swap(c[child], c[parent]); child = parent; parent = ((child - 1) >> 1); } else return; } } //向下调整 void AdjustDown(int parent) { int child = parent * 2 + 1; while (child < c.size()) { // 找以parent为根的较大的孩子 if (child + 1 < c.size() && Com(c[child], c[child+1])) child += 1; // 检测双亲是否满足情况 if (Com(c[parent], c[child])) { std::swap(c[child], c[parent]); parent = child; child = parent * 2 + 1; } else return; } } private: Container c; Compare Com; }; };
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/1 4:52:10

数据科学与大数据技术毕业设计创新的选题怎么选

1 引言 毕业设计是大家学习生涯的最重要的里程碑&#xff0c;它不仅是对四年所学知识的综合运用&#xff0c;更是展示个人技术能力和创新思维的重要过程。选择一个合适的毕业设计题目至关重要&#xff0c;它应该既能体现你的专业能力&#xff0c;又能满足实际应用需求&#xff…

作者头像 李华
网站建设 2026/5/1 2:42:26

智能科学与技术毕业设计易上手开题汇总

文章目录&#x1f6a9; 1 前言1.1 选题注意事项1.1.1 难度怎么把控&#xff1f;1.1.2 题目名称怎么取&#xff1f;1.2 选题推荐1.2.1 起因1.2.2 核心- 如何避坑(重中之重)1.2.3 怎么办呢&#xff1f;&#x1f6a9;2 选题概览&#x1f6a9; 3 项目概览题目1 : 基于协同过滤的电影…

作者头像 李华
网站建设 2026/5/1 3:47:37

从崩溃到稳定:R-Python环境版本同步的7个核心步骤(内部资料流出)

第一章&#xff1a;从崩溃到稳定&#xff1a;R-Python环境版本同步的背景与挑战在数据科学项目中&#xff0c;R 与 Python 的协同使用日益普遍。然而&#xff0c;当两个语言环境的版本不一致时&#xff0c;常导致依赖包冲突、函数调用失败甚至整个分析流程崩溃。尤其在团队协作…

作者头像 李华
网站建设 2026/5/1 3:49:41

单例设计模式(菜鸟教程)--高频面试问题系列

目录 作者有话要说&#xff1a; 一&#xff0c;什么是单例设计模式&#xff1f; 二&#xff0c;单例设计模式有什么特点&#xff1f; 三&#xff0c;什么使用使用单例设计模式&#xff1f; 四&#xff0c;单例设计模式的实现方式? 1.懒汉式的实现方式 2.饿汉式的实现方…

作者头像 李华