LIFO stack
stack是一个容器适配器类型,它被设计成LIFO(最后一个进入栈的,第一个出),并且元素的插入和提取操作只能在容器的尾部。
stack作为容器适配器, 用一个具体的容器类的封装对象作为其底层容器,提供一个具体的访问容器元素的成员函数集合。元素都是从具体的容器的“尾部”进行推送(push)和拉出(pop)操作,这个“尾部”就是所谓的栈顶(top)。
底层容器可以是任何的标准容器类模板或者其他的具体的设计容器类。仅有的要求是需要支持下面的操作:
- back()
- push_back()
- pop_back()
因此可以使用vector,deque和list这样标准容器类模板。默认的,如果对一个特殊的栈类没有指定容器类,使用deque标准类模板。
在c++标准的模板类的定义中,stack是有两个模板参赛的模板:
template<class T, class Container = deque<T> > class stack;
参数:
T:元素类型
Container:用于存储和访问元素的底层容器类型。
stack::stack
explicit stack( const Container& ctnr = Container() );
栈的构造函数
构造一个栈容器适配器对象。
一个容器适配器保持容器对象为数据。如果有容器对象,这个对象是传递给构造函数参数的一个拷贝,否则,是一个空的容器。
参数:
cntr
容器对象。
Container是第二个类模板参数(stack的底层容器的类型,默认为deque<T>)。
实例:
#include <iostream>
#include <vector>
#include <deque>
#include <stack>
using namespace std;
int main(void)
{
deque<int> mydeque(2, 100);
vector<int> myvector(3, 200);
stack<int> first;
stack<int> second(mydeque);
stack<int, vector<int> > third;
stack<int, vector<int> > fourth(myvector);
cout << "size of first: " << first.size() << endl;
cout << "size of second: " << second.size() << endl;
cout << "size of third:" << third.size() << endl;
cout << "size of fourth: " << fourth.size() << endl;
return 0;
}
执行结果:
liujl@liujl-Rev-1-0:~/mycode/STL$ ./stack
size of first: 0
size of second: 2
size of third:0
size of fourth: 3
stack::empty
bool empty() const;测试容器是否为空
返回是否栈为空,例如,size是否为0。
这个成员函数实际上调用的是同名的底层容器对象的成员函数。
实例:
#include <iostream>
#include <stack>
using namespace std;
int main(void)
{
stack<int> mystack;
int sum(0);
for (int i=1; i<=10; i++)
mystack.push(i);
while(!mystack.empty())
{
sum += mystack.top();
mystack.pop();
}
cout << "total: " << sum << endl;
return 0;
}
执行结果:
liujl@liujl-Rev-1-0:~/mycode/STL$ ./stack_empty
total: 55
stack::push
void push( const T& x);
在栈顶增加一个心的元素,在现在的栈顶元素之上。新的元素内容初始化为x的一个拷贝。
这个成员函数实际上调用底层容器对象的push_back成员函数。
参数
x
需要拷贝给新元素的值。
T是第一个模板参数(栈中存储的元素类型)
stack::pop
void pop();移除元素。
移除栈顶的元素,实际上size减1。在弹出之前调用stack::top成员函数,可以获得此元素值。
这个成员函数实际上调用底层容器对象的pop_back函数(例如deque::pop_back)。
push/pop实例:
#include <iostream>
#include <stack>
using namespace std;
int main(void)
{
stack<int> mystack;
for (int i=0; i<5; i++)
mystack.push(i);
cout << "Pop up elements: "<< endl;
while (!mystack.empty())
{
cout << " "<<mystack.top();
mystack.pop();
}
cout << endl;
return 0;
}
执行结果:
liujl@liujl-Rev-1-0:~/mycode/STL$ ./stack_pop_push
Pop up elements:
4 3 2 1 0
stack::size
size_type size() const;返回在栈中的元素个数。
stack::top
value_type& top();const value_type& top() const;
访问下一个元素
返回栈中下一个元素的引用。由于栈是LIFO容器,所以此元素是栈中最后一个push进去的元素。
附件(stl_stack.h中stack类定义)
#ifndef __SGI_STL_INTERNAL_STACK_H
#define __SGI_STL_INTERNAL_STACK_H
#include <sequence_concepts.h>
__STL_BEGIN_NAMESPACE
// Forward declarations of operators == and <, needed for friend declaration.
template <class _Tp,
class _Sequence __STL_DEPENDENT_DEFAULT_TMPL(deque<_Tp>) >
class stack;
template <class _Tp, class _Seq>
bool operator==(const stack<_Tp,_Seq>& __x, const stack<_Tp,_Seq>& __y);
template <class _Tp, class _Seq>
bool operator<(const stack<_Tp,_Seq>& __x, const stack<_Tp,_Seq>& __y);
template <class _Tp, class _Sequence>
class stack {
// requirements:
__STL_CLASS_REQUIRES(_Tp, _Assignable);
__STL_CLASS_REQUIRES(_Sequence, _BackInsertionSequence);
typedef typename _Sequence::value_type _Sequence_value_type;
__STL_CLASS_REQUIRES_SAME_TYPE(_Tp, _Sequence_value_type);
#ifdef __STL_MEMBER_TEMPLATES
template <class _Tp1, class _Seq1>
friend bool operator== (const stack<_Tp1, _Seq1>&,
const stack<_Tp1, _Seq1>&);
template <class _Tp1, class _Seq1>
friend bool operator< (const stack<_Tp1, _Seq1>&,
const stack<_Tp1, _Seq1>&);
#else /* __STL_MEMBER_TEMPLATES */
friend bool __STD_QUALIFIER
operator== __STL_NULL_TMPL_ARGS (const stack&, const stack&);
friend bool __STD_QUALIFIER
operator< __STL_NULL_TMPL_ARGS (const stack&, const stack&);
#endif /* __STL_MEMBER_TEMPLATES */
public:
typedef typename _Sequence::value_type value_type;
typedef typename _Sequence::size_type size_type;
typedef _Sequence container_type;
typedef typename _Sequence::reference reference;
typedef typename _Sequence::const_reference const_reference;
protected:
_Sequence c;
public:
stack() : c() {}
explicit stack(const _Sequence& __s) : c(__s) {}
bool empty() const { return c.empty(); }
size_type size() const { return c.size(); }
reference top() { return c.back(); }
const_reference top() const { return c.back(); }
void push(const value_type& __x) { c.push_back(__x); }
void pop() { c.pop_back(); }
};
template <class _Tp, class _Seq>
bool operator==(const stack<_Tp,_Seq>& __x, const stack<_Tp,_Seq>& __y)
{
return __x.c == __y.c;
}
template <class _Tp, class _Seq>
bool operator<(const stack<_Tp,_Seq>& __x, const stack<_Tp,_Seq>& __y)
{
return __x.c < __y.c;
}
#ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER
template <class _Tp, class _Seq>
bool operator!=(const stack<_Tp,_Seq>& __x, const stack<_Tp,_Seq>& __y)
{
return !(__x == __y);
}
template <class _Tp, class _Seq>
bool operator>(const stack<_Tp,_Seq>& __x, const stack<_Tp,_Seq>& __y)
{
return __y < __x;
}
template <class _Tp, class _Seq>
bool operator<=(const stack<_Tp,_Seq>& __x, const stack<_Tp,_Seq>& __y)
{
return !(__y < __x);
}
template <class _Tp, class _Seq>
bool operator>=(const stack<_Tp,_Seq>& __x, const stack<_Tp,_Seq>& __y)
{
return !(__x < __y);
}
#endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */
__STL_END_NAMESPACE
#endif /* __SGI_STL_INTERNAL_STACK_H */