当前位置:网站首页>stack-queue
stack-queue
2022-08-08 05:33:00 【'派派'】
1. stack的介绍和使用
1.1 介绍:stack - C++ Reference
empty:判空操作
back:获取尾部元素操作
push_back:尾部插入元素操作
pop_back:尾部删除元素操作3.标准容器vector、deque、list均符合这些需求,默认情况下,如果没有为stack指定特定的底层容器默认情况下使用deque。
1.2栈的使用

stack<int>stk;
stk.push(1);
stk.push(2);
stk.push(3);
while (!stk.empty()) {
cout << stk.top() << " ";
stk.pop();
}2. queue的介绍和使用
2.1 queue的介绍 queue - C++ Reference
2.2queue的使用

3. 容器适配器



3.1栈的模拟实现
template<class T, class Container = deque<T>>
class Stack
{
public:
void push(const T& x)
{
_con.push_back(x);
}
void pop()
{
_con.pop_back();
}
const T& top()
{
return _con.back();
}
size_t size()
{
return _con.size();
}
bool empty()
{
return _con.empty();
}
private:
Container _con;
};
void test1()
{
Stack<int,vector<int>> s;//此处用vector进行适配
s.push(3);
s.push(2);
s.push(7);
s.push(5);
while (!s.empty())
{
cout << s.top() << " ";
s.pop();
}
cout << endl;
}队列的模拟也是类似,这就不在叙述。
3.2deque的原理介绍
deque(双端队列):是一种双开口的"连续"空间的数据结构,deque并不是真正连续的空间,而是由一段段连续的小空间拼接而成的,实际deque类似于一个动态的二维数组。
1. stack和queue不需要遍历(因此stack和queue没有迭代器),只需要在固定的一端或者两端进行操作。
2. 在stack中元素增长时,deque比vector的效率高(扩容时不需要搬移大量数据);queue中的元素增长
时,deque不仅效率高,而且内存使用率高。4.priority_queue的介绍和使用
4.1介绍
empty():检测容器是否为空
size():返回容器中有效元素个数
front():返回容器中第一个元素的引用
push_back():在容器尾部插入元素
pop_back():删除容器尾部元素4.需要支持随机访问迭代器,以便始终在内部保持堆结构。
5. 标准容器类vector和deque满足这些需求。默认情况下,如果没有为特定的priority_queue类实例化指定容器类,则使用vector。
4.2priority_queue的使用

4.3 priority_queue的模拟实现
先来了解一下什么是仿函数
template<class T>
struct Greate
{
bool operator()(const T& x, const T& y) const
{
return x > y;
}
};
int main()
{
Greate<int> gg;
cout << gg(6, 3) << endl;
return 0;
}
对象可以像调用函数一样去使用
再看下sort

struct Goods
{
string _name;
double _price;
size_t _saleNum;
};
struct LessPrice
{
bool operator()(const Goods& g1, const Goods& g2) const
{
return g1._price < g2._price;
}
};
int main()
{
Goods gds[4] = { { "苹果", 2.1, 1000}, { "香蕉", 3.0, 200}, { "橙子", 2.2,300}, { "菠萝", 1.5,50} };
sort(gds, gds + 4, LessPrice());
return 0;
}结果:

模拟实现:
namespace LF
{
template<class T>
struct Less
{
public:
bool operator()(const T& x, const T& y) const
{
return x < y;
}
};
template<class T>
struct Greate
{
bool operator()(const T& x, const T& y) const
{
return x > y;
}
};
template<class T, class Container = vector<int>, class compare = Less<T>>//默认情况是大推
class Priority_queue
{
public:
void Adjustup(int child)
{
Compare comFunc;//用那个仿函数创建对象
int parent = (child - 1) / 2;
while (child > 0)
{
if (comFunc(_con[parent], _con[child]))//重载了()
{
swap(_con[parent], _con[child]);
child = parent;
parent = (child - 1) / 2;
}
else
{
break;
}
}
}
void AdjustDown(int parent)
{
Compare comFunc;
size_t child = parent * 2 + 1;
while (child < _con.size())
{
if (child + 1 < _con.size() && comFunc(_con[child], _con[child + 1]))
{
++child;
}
if (comFunc(_con[parent], _con[child]))
{
swap(_con[parent], _con[child]);
parent = child;
child = parent * 2 + 1;
}
else
{
break;
}
}
}
void push(const T& x)
{
_con.push_back(x);
AdjustUp(_con.size() - 1);
}
void pop()
{
assert(!_con.empty());
swap(_con[0], _con[_con.size() - 1]);
_con.pop_back();
AdjustDown(0);
}
const T& top()
{
return _con[0];
}
size_t size()
{
return _con.size();
}
bool empty()
{
return _con.empty();
}
private:
Container _con;
};
}
void test2()
{
LF::Priority_queue<int> p;
p.push(3);
p.push(8);
p.push(6);
p.push(4);
p.push(1);
p.push(9);
p.push(2);
while (!p.empty())
{
cout << p.top() << " ";
p.pop();
}
cout << endl;
}边栏推荐
- OLTP和OLAP问题的个人总结
- 为什么互联网大厂一边疯狂裁员,一边不停招聘?
- Session and cookie usage
- Week 8 Transformer Language Models and Implications
- 【leetcode】剑指 Offer(专项突击版)汇总
- Eighteen, OIDC OAuth2 】 【 the understanding of the application
- 【u-boot】u-boot的驱动模型分析
- How to batch import files and rename them all to the same file name
- 棋盘染色问题
- postman---postman参数化
猜你喜欢

Unity-CharacterController(角色控制器)

postman---postman parameterization

Spark entry learning-3-SparkSQL data abstraction

走进音视频的世界——RGB与YUV格式

Unity-CharacterController (Character Controller)

线索二叉树

【图像处理】matlab基础图像处理 | 图像载入、图像添加噪声、图像滤波、图像卷积

千亿级、大规模:腾讯超大 Apache Pulsar 集群性能调优实践

clue binary tree

Flutter 教程之高效且精美的滚动组件Slivers (教程含源码)
随机推荐
【猜拳游戏 基于Objective-C语言】
Leetcode sword 】 refers to the Offer (special commando) summary
值得收藏的几个postman特色功能帮你事半功倍!
【OAuth2】十八、OIDC的认识应用
【图像处理】matlab基础图像处理 | 图像载入、图像添加噪声、图像滤波、图像卷积
Servlet---ServletConfig类使用介绍
Web 攻击的日志分析:初学者指南
Go-Excelize API源码阅读(十)—— SetActiveSheet(index int)
基础了解虚拟 DOM
字符串题目解析
Leetcode78. Subset
棋盘染色问题
由联合体union引出的大小端问题
让你的应用完美适配平板
预处理笔记
Use of Filter
【MySQL】——事务的基本概念
OLTP和OLAP问题的个人总结
Week 8 Transformer Language Models and Implications
Typescript 命名空间