stack
stack栈具有后进先出的特点
#pragma once
#include<iostream>
#include<vector>
#include<list>
#include<queue>
using namespace std;
namespace rmrf
{
template<class T, class Container=deque<T>>
class stack
{
public:
bool empty()const
{
return _con.empty();
}
size_t size()const
{
return _con.size();
}
const T& top()const
{
return _con.back();
}
void push_back(const T& x)
{
_con.push_back(x);
}
void pop()
{
_con.pop_back();
}
private:
Container _con;
};
void test()
{
stack<int,string> s;
s.push_back(1);
s.push_back(2);
s.push_back(3);
s.push_back(4);
while(!s.empty())
{
cout << s.top() << " ";
s.pop();
}
}
}
queue
queue队列具有先进先出的特点
#pragma once
#include<iostream>
#include<vector>
#include<list>
#include<queue>
using namespace std;
namespace rmrf
{
template<class T, class Container>
class queue
{
public:
bool empty()const
{
return _con.empty();
}
size_t size()const
{
return _con.size();
}
const T& front()const
{
return _con.front();
}
const T& back()const
{
return _con.back();
}
void push(const T& x)
{
_con.push_back(x);
}
void pop()
{
_con.pop_front();
}
private:
Container _con;
};
void test1()
{
queue<int, list<int>> s;
s.push(1);
s.push(2);
s.push(3);
s.push(4);
while (!s.empty())
{
cout << s.front() << " ";
s.pop();
}
}
}
适配器

stack和queue都将deque作为其容器适配器
deque
deque叫做双端队列,是一种双开口的连续的的数据结构,适合在头部和尾部进行插入和删除操作,它融合了vector和list的优点
vector的优点:可以访问任意位置的下标
vector的缺点:头部的插入删除极为不便,扩容效率低,空间浪费
list的优点:任意位置的插入删除,按需申请释放空间
list的缺点:不能随机访问数的位置
deque和vector,list做适配器对比

priority_queue
默认是大堆的优先级高———默认给的仿函数是less
如果要控制小堆的优先级高——给一个greater的仿函数
//用vector作为底层容器,内部构造大堆结构。
priority_queue<int, vector<int>, less<int>> q1;
//用vector作为底层容器,内部构造小堆结构。
priority_queue<int, vector<int>, greater<int>> q2;
//不指定底层容器和内部需要构造的堆结构。
priority_queue<int> q3;
构造函数
一种是默认的构造函数,一种是迭代器区间构造的构造函数(构造成大堆)
priority_queue()
{
}
template<class InputIterator>
priority_queue(InputIterator first, InputIterator last)
:_con(first, last)
{
for (int i = (_con.size() - 1 - 1) / 2; i >= 0; i--)
{
adjust_down(i);
}
}
仿函数
template<class T>
struct Less
{
bool operator()(const T& x, const T& y)const
{
return x > y;
}
};
template<class T>
struct Greater
{
bool operator()(const T& x, const T& y)const
{
return x < y;
}
};
push函数
template<class T, class Container = vector<T>,class Compare=Less<T>>
void adjust_up(size_t child)
{
Compare com;
size_t parent = (child - 1) / 2;
while (child > 0)
{
if (com(_con[parent] ,_con[child] ))
{
swap(_con[child], _con[parent]);
child = parent;
parent = (child - 1) / 2;
}
else
{
break;
}
}
}
void push(const T& x)
{
_con.push_back(x);
adjust_up(_con.size() - 1);
}
和堆排序那里一样,尾插之后,向上调整再次构成堆。因为不能有用户自己来决定排序,所以传个仿函数,利用仿函数来进行比较,这里的less是小于,所以要自己调整顺序
pop函数
template<class T, class Container = vector<T>,class Compare=Less<T>>
void adjust_down(size_t parent)
{
Compare com;
size_t child = parent * 2 + 1;
while (child < _con.size())
{
if (child + 1 < _con.size() && com(_con[child],_con[child + 1]))
{
++child;
}
if (com(_con[parent],_con[child]))
{
swap(_con[child], _con[parent]);
parent = child;
child = parent * 2 + 1;
}
else
{
break;
}
}
}
void pop()
{
assert(!_con.empty());
swap(_con[0], _con[_con.size() - 1]);
_con.pop_back();
adjust_down(0);
}
排升序建大堆,此时第一个数据就是最大的,如果直接删掉top位置,只能第二个数堆顶,此时直接打破了原来的大堆结构,所以先将top位置与倒数第一个数据进行交换,然后删掉top位置,再自上向下进行调整
其余函数
const T& top()
{
return _con[0];
}
size_t size()
{
return _con.size();
}
bool empty()
{
return _con.empty();
}
直接进行库里的调用即可
自己构造仿函数
当函数不用less或greater时,拿date类做例子,当date类比较大小就需要自己去写
当date类需要将时间排序
class Date
{
public:
Date(int year = 1900, int month = 1, int day = 1)
: _year(year)
, _month(month)
, _day(day)
{}
bool operator<(const Date& d)const
{
return (_year < d._year) ||
(_year == d._year && _month < d._month) ||
(_year == d._year && _month == d._month && _day < d._day);
}
bool operator>(const Date& d)const
{
return (_year > d._year) ||
(_year == d._year && _month > d._month) ||
(_year == d._year && _month == d._month && _day > d._day);
}
friend ostream& operator<<(ostream& _cout, const Date& d);
private:
int _year;
int _month;
int _day;
};
ostream& operator<<(ostream & _cout, const Date & d)
{
_cout << d._year << "-" << d._month << "-" << d._day << endl;
return _cout;
}
priority_queue<Date,vector<Date>> pq;去调用
当我们只有date*的指针,这时自己重载的大于小于就不管用了,就需要自己去写一个专门针对指针的仿函数、
using namespace std;
class Date
{
friend struct LessPDate;
public:
Date(int year = 1900, int month = 1, int day = 1)
: _year(year)
, _month(month)
, _day(day)
{}
bool operator<(const Date& d)const
{
return (_year < d._year) ||
(_year == d._year && _month < d._month) ||
(_year == d._year && _month == d._month && _day < d._day);
}
bool operator>(const Date& d)const
{
return (_year > d._year) ||
(_year == d._year && _month > d._month) ||
(_year == d._year && _month == d._month && _day > d._day);
}
friend ostream& operator<<(ostream& _cout, const Date& d);
private:
int _year;
int _month;
int _day;
};
ostream& operator<<(ostream & _cout, const Date & d)
{
_cout << d._year << "-" << d._month << "-" << d._day << endl;
return _cout;
}
struct LessPDate
{
bool operator()(Date* x, Date* y)
{
return *x < *y;
}
};
void test_priority_queue2()
{
priority_queue<Date*,vector<Date*>,LessPDate> pq;
pq.push(new Date(2022, 3, 26));
pq.push(new Date(2021, 10, 26));
pq.push(new Date(2023, 3, 26));
while (!pq.empty())
{
cout << *pq.top() ;
pq.pop();
}
cout << endl;
}
770

被折叠的 条评论
为什么被折叠?



