目录
stack的模拟实现
namespace gss{
template<class T, class Container = deque<T>>
//stack的实现就是把vector包装了一下
//形成了一种新的结果:适配器
class stack{
public:
stack(){
}
void push(const T& val){
_con.push_back(val);
}
void pop(){//出栈
_con.pop_back();
}
T& top(){//获取栈顶元素
return _con.back();
}
const T& top()const{
return _con.back();
}
size_t size()const{//获取栈中元素个数
return _con.size();
}
bool empty(){//判空
return _con.empty();
}
private:
Container _con;
};
}
queue的模拟实现
namespace gss{
//队列是重新将list重新包装
//形成适配器
template<class T, class Container = deque<T>>
class queue{
public:
queue(){
}
void push(const T& val){//尾插
_con.push_back(val);
}
void pop(){//头删
_con.pop_front();
}
T& back(){//获取队尾元素
return _con.back();
}
T& front(){//获取队头元素
return _con.front();
}
const T& back()const{
return _con.back();
}
const T& front()const{
return _con.front();
}
size_t size()const{//获取队列元素个数
return _con.size();
}
bool empty(){
return _con.empty();
}
private:
Container _con;
};
}
priority_queue的使用与模拟实现
使用
存储内置类型
在文档中,priority_queue默认是按照less的方式比较,构建的是大堆,如下:
2.如果想构建小堆,那么则需要改变比较方式。结合模板参数列表,比较方式必须放到模板参数列表的第三个位置,次序不能乱。使用标准库函提供的greater即可,代码如下:
存储自定义类型
priority_queue的内部默认是按照小于的方式进行比较的,存储自定义类型的对象时,不能直接按照小于的方式进行比较,需要用户重载"<"。
如下初始化三个日期类对象,然后将其放入优先级队列中:
#include <stdio.h>
#include <queue>
#include <functional>//注意包含头文件
using namespace std;
class Date{
public:
Date(int year = 0, int month = 0, int day = 0)
:_year(year)
, _month(month)
, _day(day)
{
}
bool operator<(const Date& d)const{//必须都给成const,不然编译不能通过
if (_year<d._year || _year == d._year&&_month<d._month || _year == d._year&&_month == d._month&&_day<d._day){
return true;
}
return false;
}
bool operator>(const Date& d)const{//必须都给成const,不然编译不能通过
if (_year>d._year || _year == d._year&&_month>d._month || _year == d._year&&_month == d._month&&_day>d._day){
return true;
}
return false;
}
private:
int _year;
int _month;
int _day;
};
int main(){
Date d2(2022, 4, 18);
Date d3(2022, 4, 19);
Date d4(2022, 4, 20);
priority_queue<Date> q;
q.push(d2);
q.push(d3);
q.push(d4);
return 0;
}
打开监视:
如果想构建小堆,那么只需要使用greater的比较方式即可。
如果只是想存储自定义类型的地址,则不能在使用库中的less和greater作为默认的比较方式了,因为如果使用了库中的默认比较方式 ,它们会按照函数地址的大小进行比较。那么该怎么做?
需要用户自己提供比较方式,一般情况下,有三种比较方式:
1.函数指针,需要在类中重载"<"或">"运算符
2.仿函数,,需要在类中重载"<"或">"运算符
3.lambda表达式(后续讲)
模拟实现
模拟实现就是将堆算法与vevtor进行了包装,堆算法在前面已经讲解过,在这里就不细讲了,具体实现代码如下:
namespace ck{
//优先级队列就是将vector和堆算法进行了包装,成为容器适配器
template<class T,class Container=vector<int>,class Com=less<T>>
class priority_queue{
public:
priority_queue(){
}
template <class iterator>
priority_queue(iterator first, iterator last)//给定区间的构造方法
:_con(first,last)
{
for (int parent = (_con.size() - 2) / 2; parent >= 0;parent--){//建堆
HeapAdjustDown(parent);
}
}
void push(const T& val){//插入元素
_con.push_back(val);
HeapAdjustUp(_con.size()-1);
}
void pop(){//删除元素
if (_con.empty()){
return;
}
std::swap(_con.front(),_con.back());
_con.pop_back();
HeapAdjustDown(0);
}
const T& top(){//获取堆顶元素
return _con.front();
}
size_t size(){//获取元素个数
return _con.size();
}
bool empty(){
return _con.empty();
}
private:
void HeapAdjustUp(int child){//向上调整
int parent = (child - 1) / 2;
Com com;
while (child){
if (com(_con[parent],_con[child])){
std::swap(_con[parent],_con[child]);
child = parent;
parent = (child - 1) / 2;
}
else{
return;
}
}
}
void HeapAdjustDown(int parent){//向下调整
int child = parent * 2 + 1;
int size = _con.size();
Com com;
while (child<size){
if (child+1<size&&com(_con[child],_con[child+1])){
child += 1;
}
if (com(_con[parent], _con[child])){
std::swap(_con[child],_con[parent]);
parent = child;
child = parent * 2 + 1;
}
else{
return;
}
}
}
Container _con;//默认是vector<int> 类型
};
}
容器适配器
什么是适配器
deque原理介绍
deque的优缺点
优点
与vector比较,deque的优势是:头部插入和删除时,不需要搬移元素,效率特别高,而且在扩容时,也不 需要搬移大量的元素,因此其效率是必vector高的。 与list比较,其底层是连续空间,空间利用率比较高,不需要存储额外字段。
缺点
deque有一个致命缺陷:不适合遍历,因为在遍历时,deque的迭代器要频繁的去检测其是否移动到 某段小空间的边界,导致效率低下,而序列式场景中,可能需要经常遍历,因此在实际中,需要线性结构 时,大多数情况下优先考虑vector和list,deque的应用并不多,而目前能看到的一个应用就是,STL用其作为stack和queue的底层数据结构
stack和queue的底层结构