stack是作为容器适配器被实现的,容器适配器即是对特定类封装作为其底层的容器,并提供一组特定的成员函数来访问其元素,将特定类作为其底层的,元素特定容器的尾部(即栈顶)被压入和弹出
stack的底层容器可以是任何标准的容器类模板或者一些其他特定的容器类,这些容器类应该支持以下操作:
empty:判空操作
back:获取尾部元素操作
push_back:尾部插入元素操作
pop_back:尾部删除元素操作
标准容器vector、deque、list均符合这些需求,默认情况下,如果没有为stack指定特定的底层容器,默认情况下使用deque:



stack的模拟实现- #pragma once
- #include
-
- namespace djx
- {
- template<class T,class Container = deque
> - 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;
- };
- }
stack默认的底层容器是双端队列deque(名字有队列却不是队列)我们也可以用vector,list作为底层容器
测试:
- #include
- #include
- #include
- using namespace std;
- #include"stack.h"
-
- int main()
- {
- djx::stack<int>st;//ok 底层容器默认为deque
- //djx::stack
>st;//ok 底层容器为list - //djx::stack
>st;//ok 底层容器为vector -
- st.push(1);
- st.push(2);
- st.push(3);
- st.push(4);
-
- while (!st.empty())
- {
- cout << st.top() << " ";
- st.pop();
- }
- cout << endl;
- return 0;
- }

队列作为容器适配器实现,容器适配器即将特定容器类封装作为其底层容器类,queue提供一组特定的成员函数来访问其元素。元素从队尾入队列,从队头出队列
底层容器可以是标准容器类模板之一,也可以是其他专门设计的容器类。该底层容器应至少支持以下操作:
empty:检测队列是否为空
size:返回队列中有效元素的个数
front:返回队头元素的引用
back:返回队尾元素的引用
push_back:在队列尾部入队列
pop_front:在队列头部出队列
标准容器类deque和list满足了这些要求。默认情况下,如果没有为queue实例化指定容器类,则使用标准容器deque,注意queue的底层容器不适合用vector,因为vector没有提供pop_front
- #include
-
- namespace djx
- {
- template<class T,class Container = deque
> - class queue
- {
- public:
- void push(const T& x)
- {
- _con.push_back(x);
- }
-
- void pop()
- {
- _con.pop_front();
- }
-
- const T& front()
- {
- return _con.front();
- }
-
- const T& back()
- {
- return _con.back();
- }
-
- size_t size()
- {
- return _con.size();
- }
-
- bool empty()
- {
- return _con.empty();
- }
- private:
- Container _con;
- };
- }
测试:
- djx::queue<int> q;
- //djx::queue
> q; //ok -
- q.push(1);
- q.push(2);
- q.push(3);
- q.push(4);
-
- while (!q.empty())
- {
- cout << q.front() << " ";
- q.pop();
- }
- cout << endl;
![]()
总结:虽然stack和queue中也可以存放元素,但在STL中并没有将其划分在容器的行列,而是将其称为容器适配器,这是因为stack和队列只是对其他容器的接口进行了包装,STL中stack和queue默认使用deque
1. 优先队列是一种容器适配器,根据严格的弱排序标准,它的第一个元素总是它所包含的元素中最大的
2 优先级队列其实就类似于堆
3 底层容器可以是任何标准容器类模板,也可以是其他特定设计的容器类。容器应该可以通过随机访问迭代器访问,并支持以下操作:
empty():检测容器是否为空
size():返回容器中有效元素个数
front():返回容器中第一个元素的引用
push_back():在容器尾部插入元素
pop_back():删除容器尾部元素
标准容器类vector和deque满足这些需求。默认情况下,如果没有为特定的priority_queue类实例化指定容器类,则使用vector
优先级队列默认使用vector作为其底层存储数据的容器,在vector上又使用了堆算法将vector中元素构造成堆的结构,因此priority_queue就是堆,所有需要用到堆的位置,都可以考虑使用priority_queue。注意:默认情况下priority_queue是大堆
- #pragma once
- #include
- namespace djx
- {
- template<class T,class Container = vector
,class Compare = less> - class priority_queue
- {
- public:
- priority_queue()
- {}
-
- template <class InputIterator>
- priority_queue(InputIterator first, InputIterator last)
- :_con(first,last)
- {
- //向下调整建堆
- for (int i = (_con.size() - 2) / 2; i >= 0; i--)
- {
- adjust_down(i);
- }
- }
-
- void adjust_up(int child)//向上调整建堆
- {
- Compare com;
- int parent = (child - 1) / 2;
- while (child > 0)
- {
- if (com(_con[parent], _con[child]))//使用仿函数(函数对象)com,实质是com对象调用了重载运算符(),即com.operator()(_con[parent,_con[child])
- {
- swap(_con[parent], _con[child]);
- child = parent;
- parent = (child - 1) / 2;
- }
- else
- {
- break;
- }
- }
- }
-
- void adjust_down(int 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[parent], _con[child]);
- parent = child;
- child = parent * 2 + 1;
- }
- else
- {
- break;
- }
- }
- }
-
- void push(const T&x)
- {
- _con.push_back(x);
- adjust_up(_con.size() - 1);
- }
-
- void pop()//删除堆顶元素
- {
- swap(_con[0], _con[_con.size() - 1]);
- _con.pop_back();
- adjust_down(0);
- }
-
- const T& top()
- {
- return _con[0];
- }
-
- size_t size()
- {
- return _con.size();
- }
-
- bool empty()
- {
- return _con.empty();
- }
-
- private:
- Container _con;
- };
- }
这里的less和greater均使用了库中的

测试:
- int main()
- {
- djx::priority_queue<int> q;//默认为大堆
- //djx::priority_queue
,greater> q; //小堆 - q.push(1);
- q.push(5);
- q.push(2);
- q.push(8);
-
- while (!q.empty())
- {
- cout << q.top() << " ";
- q.pop();
- }
- cout << endl;
- return 0;
- }

小堆结果:
这里使用了仿函数com,也称其为函数对象,本质上仿函数也还是一个对象,只是这种对象重载了()运算符,让其看起来和函数调用一样 ,如上面的less和greater我们可以不用库中的,替换成自己的Less,Greater即可
- //仿函数
- template<class T>
- class Less
- {
- public:
- bool operator()(const T& x, const T& y)
- {
- return x < y;
- }
-
- };
-
- template <class T>
- class Greater
- {
- public:
- bool operator()(const T& x, const T& y)
- {
- return x > y;
- }
- };
- Less<int> less1; // 函数对象
- cout << less1(2, 3) << endl;
- //cout << less1.operator()(2, 3) << endl; //等价上面的less1(2,3)
仿函数的用途:
以日期类自定义对象为例 ,用优先级队列找出最大的日期
- 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;
- return _cout;
- }
- djx::priority_queue
pq; - pq.push(Date(2023, 10, 1));
- pq.push(Date(2023, 10, 2));
- pq.push(Date(2023, 10, 3));
- cout << pq.top() << endl;
但若我们就想用每个日期对象的地址去找出最大的日期,即优先级队列中存储的是Date*
那么结果就不太对了,一会是10.1 一会是10.3
- djx::priority_queue
pq;//这样写无法得出正确答案 - pq.push(new Date(2023, 10, 1));
- pq.push(new Date(2023, 10, 2));
- pq.push(new Date(2023, 10, 3));
- cout << *(pq.top()) << endl;
这时候仿函数就可以上场发挥作用了,有了仿函数我们在比较大小的时候可以根据自己的想法去设计比较内容
- class PDateCompare//仿函数
- {
- public:
- bool operator()(Date* p1, Date* p2)
- {
- return *p1 < *p2;
- }
- };
- djx::priority_queue
,PDateCompare> pq;//要传第三个参数,必须先传第二个参数 - pq.push(new Date(2023, 10, 1));
- pq.push(new Date(2023, 10, 2));
- pq.push(new Date(2023, 10, 3));
- cout << *(pq.top()) << endl;
deque(双端队列):是一种双开口的"连续"空间的数据结构,双开口的含义是:可以在头尾两端进行插入和删除操作,且时间复杂度为O(1),与vector比较,头插效率高,不需要搬移元素;与list比较,空间利用率比较高
实际中deque不常用,但是头尾插入删除效率不错,略优于vector和list
deque并不是真正连续的空间,而是由一段段连续的小空间拼接而成的,实际deque类似于一个动态的二维数组,而且deque的底层结构比较复杂
与vector比较,deque的优势是:头部插入和删除时,不需要搬移元素,效率特别高,而且在扩容时,也不需要搬移大量的元素
与list比较,其底层是连续空间,空间利用率比较高
但是,deque有一个致命缺陷:不适合遍历,因此在实际中,需要线性结构时,大多数情况下优先考虑vector和list,deque的应用并不多,而目前能看到的一个应用就是,STL用其作为stack和queue的底层数据结构
为什么选择deque作为stack和queue的底层默认容器?
1. stack和queue不需要遍历
2 在stack中元素增长时,deque比vector的效率高(扩容时不需要搬移大量数据);queue中的元素增长时,deque不仅效率高,而且内存使用率高