模板类 stack 的理解 积累
来源:互联网 发布:灵界室友网络剧下载 编辑:程序博客网 时间:2024/05/21 17:33
template<class T, class Container = deque<T> > class stack;
参数:
T:元素类型
Container:用于存储和访问元素的底层容器类型。
STL学习之stack适配器
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 */
STL源码剖析1
stl概论
stl以抽象概念为主题而非以实际类为主的结构,形成了一个严谨的接口标准。在此接口之下,任何组件都有最大的独立性,并以所谓迭代器胶合起来,或以所谓配接器互相配接,或以所谓仿函数动态选择某种策略。
C++允许我们自行定义型别,C++template允许我们将型别参数化,藉由两者结合并透过traits变成技法形成STL。
stl六大组件:containers,algorithms,iterators,functors,adapters,allocators。
template<class T, class Sequence = deque<T> >
class stack
{
// friend bool operator == <T> (const stack<T>&, const stack<T>&); // OK
// friend bool operator == <T> (const stack&, const stack&); // OK
friend bool operator == <> (const stack&, const stack&); // OK
friend bool operator < <> (const stack&, const stack&); //<> 不理解不理解啊
// friend bool operator == (const stack&, const stack&); // error
};
..... // 实现省略
这种奇特的语法是为了实现所谓的 bound friend templates,也就是所说 class template 的某个具体实现与friend function template的某个具现体有一对一的关系。
int main()
{
stack<int> x;
stack<int> y;
cout << (x == y) << endl; // OK
stack<char> y1;
cout << (x == y1) << endl; // error
}
解答:(侯捷:STL组态解释部分p32)
组态: __STL_NULL_TMPL_ARGS (bound friend template friend)
定义如下 为 <>
就是所谓的class template 的某个具现体(instantiation)与其friend function template 的某个具现体有一对一的关系。
- 模板类 stack 的理解 积累
- 自己实现的stack模板类
- C++:栈(stack)的模板类实现
- c++ stack模板类
- 类模板stack小试牛刀
- 模板类实现stack
- 模板类stack
- Stack类模板
- 类模板 stack
- Stack--C++模板类
- C++stack模板类
- Stack类模板
- stack的理解
- Stack数据结构的模板实现
- 动态数组的Stack类模板的实现
- 栈(stack)的模板类及成员函数的实现
- OJ积累--求类中数据成员的最大值-类模板
- C++之Stack模板类
- 收集关于Github的一些资料
- 算是第一天吧【健身】
- makefile 模版
- shell实例浅谈之判断是可执行文件并存在PATH路径中
- nginx的spdy协议
- 模板类 stack 的理解 积累
- 【官方文档】Cocos2d-x 3.0 Json用法
- 学习SinglePHP
- hadoop2的伪分布式安装
- 32-UITableView基本用法
- hive整合hbase并做测试
- linux下常用命令wget进行整站下载
- Shell 常用命令
- PHP与Mysql使用小结。