SGI STL stack系以deque作为底部容器完成其所有工作,而具有这种“修改某物接口,
形成另一种风貌”之性质者,称为适配器(adapter),stack被归类为 container adapter,
这种容器适配器还有queue, priority_queue,它们没有迭代器。
适用情况:
使用一个已经存在的类,如果它的接口和你实际要求的不一致时,可以考虑使用适配器模式
#include <iostream>
using namespace std;
class Deque
{
public:
void push_back(int x) { cout << "push_back " << x << endl; }
void pop_back() { cout << "pop_back" << endl; }
void push_front(int x) { cout << "push_front " << x << endl; }
void pop_front() {cout << "pop_front" << endl; }
};
class Stack
{
public:
void push(int x) { sequence.push_back(x); }
void pop() { sequence.pop_back(); }
private:
Deque sequence;
};
int main()
{
Stack st;
st.push(10);
st.pop();
return 0;
}