news 2026/8/7 23:48:04

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/8/7 23:05:57

【安全专家亲授】私有化Dify的SSL配置秘诀:保障数据传输不被窃取

第一章&#xff1a;私有化 Dify 的 SSL 配置在私有化部署 Dify 时&#xff0c;启用 HTTPS 是保障通信安全的关键步骤。通过配置 SSL 证书&#xff0c;可以确保前端与后端之间的数据传输加密&#xff0c;防止中间人攻击和敏感信息泄露。通常使用 Nginx 作为反向代理服务器来实现…

作者头像 李华
网站建设 2026/8/7 13:13:50

Vue3+JS 高级前端面试题

题目 1&#xff1a;Vue3 响应式边界问题与复杂状态管理&#xff08;电商购物车场景&#xff09;问题在 Vue3 电商项目的购物车模块中&#xff0c;存在以下场景&#xff1a;购物车数据为深层嵌套对象&#xff08;{ list: [{ goods: { sku: [], price: 0 }, count: 1 }], selecte…

作者头像 李华
网站建设 2026/8/6 23:44:22

海康威视智能工厂,是如何走向“领航”的?

破解“小批量、多品种、大规模定制”制造难题。文&#xff5c;徐鑫编&#xff5c;任晓渔浙江桐庐的海康威视制造基地的车间里&#xff0c;1500台移动机器人在厂房内穿梭不停&#xff0c;精准地把物料从各级仓储货架配送到线头。全自动无人化生产线上&#xff0c;AI及智能感知技…

作者头像 李华
网站建设 2026/8/7 8:12:26

《深入昇腾底层:Ascend C 编程模型与高性能算子开发实战》

1. 背景&#xff1a;为何需要 Ascend C&#xff1f;在大模型时代&#xff0c;AI 算力需求呈指数级增长。通用深度学习框架&#xff08;如 PyTorch、TensorFlow&#xff09;虽提供了丰富的高层 API&#xff0c;但在面对以下场景时往往力不从心&#xff1a;框架未支持的新型算子&…

作者头像 李华
网站建设 2026/8/7 23:05:37

实战 Ascend C:从零实现高性能自定义算子

引言&#xff1a;为什么你需要亲手写一个 Ascend C 算子&#xff1f;在 AI 工程实践中&#xff0c;我们常常遇到这样的困境&#xff1a;现有深度学习框架提供的算子无法满足特定需求——可能是精度要求更高、可能是计算模式特殊、也可能是性能瓶颈卡在某个环节。此时&#xff0…

作者头像 李华
网站建设 2026/8/7 1:00:39

掌握这3种R包,轻松完成空间转录组细胞轨迹建模!

第一章&#xff1a;空间转录组的 R 语言细胞轨迹分析空间转录组技术结合了基因表达数据与组织空间位置信息&#xff0c;为解析细胞异质性和发育轨迹提供了全新视角。利用 R 语言进行细胞轨迹推断&#xff08;pseudotime analysis&#xff09;&#xff0c;能够揭示细胞在空间维度…

作者头像 李华