当前位置:网站首页>stack-queue
stack-queue
2022-08-08 05:54:00 【'pie pie'】
1. stack的介绍和使用
1.1 介绍:stack - C++ Reference
empty:判空操作
back:获取尾部元素操作
push_back:尾部插入元素操作
pop_back:尾部删除元素操作3.标准容器vector、deque、list均符合这些需求,默认情况下,如果没有为stackSpecifies a specific underlying container to use by defaultdeque.
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;
}The simulation of queues is similar,This is not the narrative.
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的模拟实现
Let’s first understand what a functor is
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;
}
Objects can be used like functions are called
再看下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>>//The default is big push
class Priority_queue
{
public:
void Adjustup(int child)
{
Compare comFunc;//Create the object with that functor
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;
}边栏推荐
- File Operations - IO
- Matlab simulation of photovoltaic mppt maximum power control based on disturbance observation method
- 【u-boot】u-boot的驱动模型分析
- VSCode已经设置过为中文但变成英文的解决办法
- Tensorboard的使用 ---- SummaryWriter类(pytorch版)
- Week 9 10 Neural Networks
- 分类任务说明
- The tests that need to be done in the development of medical device products
- 神经网络一般训练多少次,神经网络训练时间过长
- postman---postman参数化
猜你喜欢

数字IC设计笔试题汇总(四):一些基础知识点

The tests that need to be done in the development of medical device products

Completed - desktop interactive wizard design based on facial expressions (share the results, attach the data set of facial expressions and the yolov5 model trained by yourself and the interactive int

Object.prototype.toString()如何判断数据类型及注意点

【u-boot】u-boot的驱动模型分析

Unity mouse cursor usage learning

Redis 的内存策略

How to batch import files and rename them all to the same file name

LVS:NAT模式详解

Web 攻击的日志分析:初学者指南
随机推荐
【MySQL】——事务的基本概念
状态压缩复习
webstorage
【猜拳游戏 基于Objective-C语言】
tkinter-TinUI-xml实战(7)PDF分页与合并
【数学建模】微分方程求解 | dsolve函数 | ode45函数
And an array merge rank by rank
uvm简介
Rust开发——Struct使用示例
【u-boot】u-boot的驱动模型分析
Day8:面试必考编程题(细心OJ)
如何批量导入文件,并全部自定义重命名为相同文件名
阿里云的数据库怎么提高访问速度的本地的打开的方式是www.zgysffm.com怎样的?
postman---postman参数化
Postman显示验证码图片(base64字符串)
Preprocessing Notes
Promise的使用与async/await的使用
Sqlmap + dnslog injection of repetition
棋盘染色问题
postgis 数据表 迁移时错误解决方法