stack是栈,先进后出,只能一头操作;queue是队列,先进先出,一头进,另一头出。VS里的stack与queue底层都是使用双端队列deque实现的。通过改变deque的行为而生成不同的容器,这就做容器适配器。
stack
// TEMPLATE CLASS stack
template<class _Ty,
class _Container = deque<_Ty> >
class stack
{ // LIFO queue implemented with a container
public:
typedef stack<_Ty, _Container> _Myt;
typedef _Container container_type;
typedef typename _Container::value_type value_type;
typedef typename _Container::size_type size_type;
typedef typename _Container::reference reference;
typedef typename _Container::const_reference const_reference;
stack()
: c()
{ // construct with empty container
}
stack(const _Myt& _Right)
: c(_Right.c)
{ // construct by copying _Right
}
explicit stack(const _Container& _Cont)
: c(_Cont)
{ // construct by copying specified container
}
_Myt& operator=(const _Myt& _Right)
{ // assign by copying _Right
c = _Right.c;
return (*this);
}
stack(_Myt&& _Right)
: c(_STD move(_Right.c))
{ // construct by moving _Right
}
explicit stack(_Container&& _Cont)
: c(_STD move(_Cont))
{ // construct by copying specified container
}
_Myt& operator=(_Myt&& _Right)
{ // assign by moving _Right
c = _STD move(_Right.c);
return (*this);
}
void push(value_type&& _Val)
{ // insert element at beginning
c.push_back(_STD move(_Val));
}
template<class _Valty>
void emplace(_Valty&& _Val)
{ // insert element at beginning
c.emplace_back(_STD forward<_Valty>(_Val));
}
void swap(_Myt&& _Right)
{ // exchange contents with movable _Right
c.swap(_STD move(_Right.c));
}
bool empty() const
{ // test if stack is empty
return (c.empty());
}
size_type size() const
{ // test length of stack
return (c.size());
}
//栈顶元素
reference top()
{ // return last element of mutable stack
return (c.back());
}
const_reference top() const
{ // return last element of nonmutable stack
return (c.back());
}
//入栈
void push(const value_type& _Val)
{ // insert element at end
c.push_back(_Val);
}
//出栈
void pop()
{ // erase last element
c.pop_back();
}
const _Container& _Get_container() const
{ // get reference to container
return (c);
}
void swap(_Myt& _Right)
{ // exchange contents with _Right
c.swap(_Right.c);
}
protected:
_Container c; // the underlying container 底层容器,默认deque
};
queue
// TEMPLATE CLASS queue
template<class _Ty,
class _Container = deque<_Ty> >
class queue
{ // FIFO queue implemented with a container
public:
typedef queue<_Ty, _Container> _Myt;
typedef _Container container_type;
typedef typename _Container::value_type value_type;
typedef typename _Container::size_type size_type;
typedef typename _Container::reference reference;
typedef typename _Container::const_reference const_reference;
queue()
: c()
{ // construct with empty container
}
queue(const _Myt& _Right)
: c(_Right.c)
{ // construct by copying _Right container
}
explicit queue(const _Container& _Cont)
: c(_Cont)
{ // construct by copying specified container
}
_Myt& operator=(const _Myt& _Right)
{ // assign by copying _Right
c = _Right.c;
return (*this);
}
queue(_Myt&& _Right)
: c(_STD move(_Right.c))
{ // construct by moving _Right
}
explicit queue(_Container&& _Cont)
: c(_STD move(_Cont))
{ // construct by moving specified container
}
_Myt& operator=(_Myt&& _Right)
{ // assign by moving _Right
c = _STD move(_Right.c);
return (*this);
}
//入队列
void push(value_type&& _Val)
{ // insert element at beginning
c.push_back(_STD move(_Val));
}
template<class _Valty>
void emplace(_Valty&& _Val)
{ // insert element at beginning
c.emplace_back(_STD forward<_Valty>(_Val));
}
void swap(_Myt&& _Right)
{ // exchange contents with movable _Right
c.swap(_STD move(_Right.c));
}
bool empty() const
{ // test if queue is empty
return (c.empty());
}
size_type size() const
{ // return length of queue
return (c.size());
}
//front元素
reference front()
{ // return first element of mutable queue
return (c.front());
}
const_reference front() const
{ // return first element of nonmutable queue
return (c.front());
}
//back元素
reference back()
{ // return last element of mutable queue
return (c.back());
}
const_reference back() const
{ // return last element of nonmutable queue
return (c.back());
}
void push(const value_type& _Val)
{ // insert element at beginning
c.push_back(_Val);
}
//出队列
void pop()
{ // erase element at end
c.pop_front();
}
const _Container& _Get_container() const
{ // get reference to container
return (c);
}
void swap(_Myt& _Right)
{ // exchange contents with _Right
c.swap(_Right.c);
}
protected:
_Container c; // the underlying container 底层容器,默认deque
};
本文深入解析了栈(stack)和队列(queue)两种基本数据结构的特性与实现方式。介绍了栈作为后进先出(LIFO)的数据结构,仅在一端进行操作;而队列作为先进先出(FIFO)的数据结构,两端分别进行插入和删除操作。同时,文章指出在Visual Studio中,这两种数据结构底层均采用双端队列(deque)实现,并通过改变其行为来生成不同的容器,即容器适配器。
1466

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



