运用配接器模式对现有的容器类改造就可以形成stack和queue了。
stl里面的stack底层默认使用的容器为deque
template>
class stack
{
typedef _container::value_type value_type;
typedef _container::size_type size_type;
_container c;
stack(){};
void push(const value_type& t){ c.push_back(t);};
void pop(){ c.pop_back(); };
bool empty(){return c.empty();};
bool size(){return c.size();};
inline friend template
bool operator == (const stack&left, const stack&right)
{ return left.c==right.c;};
bool operator != (const stack&left, const stack&right)
{ return left.c!=right.c;};
bool operator >= (const stack&left, const stack&right)
{ return left.c>=right.c;};
bool operator <= (const stack&left, const stack&right)
{ return left.c<=right.c;};
bool operator >(const stack&left, const stack&right)
{ return left.c>right.c;};
bool operator <(const stack&left, const stack&right)
{ return left.c};
stack只有顶端的元素提供访问,因此其不提供具有走访能力的iterator;
queue的定义与stack基本一样。
template ,class COMPARE=LESS >
class priority_queue
{ _container c;
Compare t;
priority_queue(iterator first, iterator last ,COMPARE comp)(make_heap(c,begin(),c.end(),comp);)
void push(const type& ty)(c.push_back(ty),push_heap(c.begin(),c.end(),comp);)
void pop()(pop_heap(c.begin(),c.end()),c.pop_back();)
}
}
阅读(875) | 评论(0) | 转发(0) |