`
阿尔萨斯
  • 浏览: 4170745 次
社区版块
存档分类
最新评论

STL stack

 
阅读更多

stack 介绍

栈是一种容器适配器,特别为后入先出而设计的一种(LIFO ),那种数据被插入,然后再容器末端取出

栈实现了容器适配器,这是用了一个封装了的类作为他的特定容器,提供了一组成员函数去访问他的元素,元素从特定的容器,也就是堆栈的头取出袁术。

这个基础的容器可能是任何标准的容器类,和一些其他特殊设计的模板类,唯一的要求就是要支持一下的操作

  1. <spanstyle="font-size:16px;"><strong>•</strong>back()
  2. •push_back()
  3. •pop_back()</span>

因此,标准的容器类模板vector,dequelist可以使用,默认情况下,如果没有容器类被指定成为一个提别的stack 类,标准的容器类模板就是deque 队列。


实现C++ STL,栈有两个参数。

template < class T, class Container = deque<T> > class stack;

参数示意:

  • T:元素类型
  • Container: 被用于存储和访问元素的的类型

成员函数

stack::stack

explicit stack ( const Container& ctnr = Container() );

用于构造一个栈适配器对象。

ctnr
Container object
Containeris the second class template parameter (the type of the underlying container for thestack; by default:deque<T>, seeclass description).
  1. //test_stack.cpp:定义控制台应用程序的入口点。
  2. //
  3. #include"stdafx.h"
  4. #include<stack>
  5. #include<vector>
  6. #include<deque>
  7. #include<iostream>
  8. usingnamespacestd;
  9. int_tmain(intargc,_TCHAR*argv[])
  10. {
  11. deque<int>mydeque(2,100);
  12. vector<int>myvector(2,200);
  13. stack<int>first;
  14. stack<int>second(mydeque);
  15. stack<int,vector<int>>third;
  16. stack<int,vector<int>>fourth(myvector);
  17. cout<<"sizeoffirst:"<<(int)first.size()<<endl;
  18. cout<<"sizeofsecond:"<<(int)second.size()<<endl;
  19. cout<<"sizeofthird:"<<(int)third.size()<<endl;
  20. cout<<"sizeoffourth:"<<(int)fourth.size()<<endl;
  21. return0;
  22. }

output:

size of first: 0
size of second: 3
size of third: 0
size of fourth: 2

stack::empty

bool empty ( ) const;

判断是否为空。

Return Value

trueif the container size is0,falseotherwise.

  1. //stack::empty
  2. #include<iostream>
  3. #include<stack>
  4. usingnamespacestd;
  5. intmain()
  6. {
  7. stack<int>mystack;
  8. intsum(0);
  9. for(inti=1;i<=10;i++)mystack.push(i);
  10. while(!mystack.empty())
  11. {
  12. sum+=mystack.top();
  13. mystack.pop();
  14. }
  15. cout<<"total:"<<sum<<endl;
  16. return0;
  17. }

Output:

total: 55

stack::pop

void pop ( );

在栈的顶部移除元素。

  1. //stack::push/pop
  2. #include<iostream>
  3. #include<stack>
  4. usingnamespacestd;
  5. intmain()
  6. {
  7. stack<int>mystack;
  8. for(inti=0;i<5;++i)mystack.push(i);
  9. cout<<"Poppingoutelements...";
  10. while(!mystack.empty())
  11. {
  12. cout<<""<<mystack.top();
  13. mystack.pop();
  14. }
  15. cout<<endl;
  16. return0;
  17. }

Output:

Popping out elements... 4 3 2 1 0

stack::push

void push ( const T& x );

在栈顶添加元素

  1. //stack::push/pop
  2. #include<iostream>
  3. #include<stack>
  4. usingnamespacestd;
  5. intmain()
  6. {
  7. stack<int>mystack;
  8. for(inti=0;i<5;++i)mystack.push(i);
  9. cout<<"Poppingoutelements...";
  10. while(!mystack.empty())
  11. {
  12. cout<<""<<mystack.top();
  13. mystack.pop();
  14. }
  15. cout<<endl;
  16. return0;
  17. }

Output:

Popping out elements... 4 3 2 1 0

stack::size

size_type size ( ) const;

计算栈对象元素个数

  1. //stack::size
  2. #include<iostream>
  3. #include<stack>
  4. usingnamespacestd;
  5. intmain()
  6. {
  7. stack<int>myints;
  8. cout<<"0.size:"<<(int)myints.size()<<endl;
  9. for(inti=0;i<5;i++)myints.push(i);
  10. cout<<"1.size:"<<(int)myints.size()<<endl;
  11. myints.pop();
  12. cout<<"2.size:"<<(int)myints.size()<<endl;
  13. return0;
  14. }



Output:

0. size: 0
1. size: 5
2. size: 4

stack::top

      value_type& top ( );
const value_type& top ( ) const;

返回栈顶元素

  1. //test_stack.cpp:定义控制台应用程序的入口点。
  2. //
  3. #include"stdafx.h"
  4. #include<stack>
  5. #include<vector>
  6. #include<deque>
  7. #include<iostream>
  8. usingnamespacestd;
  9. int_tmain(intargc,_TCHAR*argv[])
  10. {
  11. stack<int>mystack;
  12. mystack.push(10);
  13. mystack.push(20);
  14. mystack.top()-=5;
  15. cout<<"mystack.top()isnow"<<mystack.top()<<endl;
  16. return0;
  17. }

Output:

mystack.top() is now 15

分享到:
评论

相关推荐

    心希盼 c++ STL Stack(栈)

    心希盼 c++ STL Stack(栈) 包含了用List和Vector来实现的Stack 详细说明请看“心希盼 Stack.doc”

    数据结构课程设计(链表与栈)

    里面主要有两个设计《单链表的基本操作》和《利用栈设计多种计算》 里面有大量详细的流程图和算法分析。

    C++数据结构接口(栈、队列与二叉查找树)

    C++实现了栈、队列与二叉查找树,用于学习,直接调用即可

    STL之stack栈(csdn)————程序.pdf

    STL之stack栈(csdn)————程序

    stack-stl.cpp

    stack-stl.cpp

    C++ STL Adaptor stack、queue和vector的使用.doc

    在c++中如何使用STL里的stack,queue,vector,里面有常用操作还有示意

    C++ STL容器stack和queue详解

    主要介绍了C++ STL容器stack和queue详解的相关资料,需要的朋友可以参考下

    c++stack_和_queue用法

    c++stack_和_queue用法,很好的介绍了STL中stack和queue的用法,及其使用方法

    标准模板库STL

    STL容器部分主要由头文件、、、、、&lt;stack&gt;和组成。 (2)算法(Algorithms)。包括各种基本算法,如比较、交换、查找、排序、遍历操作、复制、修改、移除、反转、合并等等。 STL算法部分主要由头文件和组成。 ...

    STL知识点思维导图

    组成的库来说提供了更好的代码重用机会。在C++标准中,STL被组织为下面的几个头文件:、、、、、、、、、、、、&lt;stack&gt;和。文件中主要介绍了前面八个的使用,并且重点介绍了他们的属性和一些成员函数的使用。

    STL源码剖析.pdg

    sgi stl 内部文件(sgi stl真正实现于此) 018 1.8.3 sgi stl 的组态设定(configuration) 019 1.9可能令你困惑的c++ 语法 026 1.9.1 stl_config.h 中的各种组态 027 组态3:static template member 027 组态5...

    STL模板库思维导图

    STL(Standard Template Library,标准模板库)是惠普实验室开发的一系列软件的统称。现然主要出现在C++中,但在被引入C++之前该...在C++标准中,STL被组织为下面的13个头文 件:、、、、、、、、、、、&lt;stack&gt; 和。

    C++标准模板库STL初步(1)

    组成的库来说提供了更好的代码重用机会。在C++标准中,STL被组织为下面的几个头文件:、、、、、、、、、、、、&lt;stack&gt;和。文件中主要介绍了前面八个的使用,并且重点介绍了他们的属性和一些成员函数的使用。

    C++ STL开发技术导引(第5章)

    第一篇 预备知识 第1章 C++编程技术 2 1.1 类和对象 2 1.2 类的继承 5 1.3 函数重载 5 1.4 访问控制 7 1.5 操作符重载 8 1.6 显式类型转换 9 1.7 异常处理 13 ...附录 STL版权说明 438

    C++标准模板库(STL) -容器

    STL的代码从广义上讲分为三类:algorithm(算法)、container(容器)和iterator(迭代器),几乎所有的代码都采用了模板类和模版函数的方式,这相比于传统的由函数和类组成的库来说提供了更好的...、、、、&lt;stack&gt;和。

    stack (STL)在VS平台下的源代码

    这里有std::stack在VS下的源码

    stl-views.gdb

    # The following STL containers are currently supported: # # std::vector&lt;T&gt; -- via pvector command # std::list&lt;T&gt; -- via plist or plist_member command # std::map,T&gt; -- via pmap or pmap_member command #...

    STL 源码剖析(侯捷先生译著)

    内容简介回到顶部↑这本书不适合C++ 初学者,不适合 Genericity(泛型技术)初学者,或 STL 初学者。这本书也不适合带领你学习面向对象(Object Oriented)技术 — 是的,STL 与面向对象没有太多关连。本书前言清楚...

Global site tag (gtag.js) - Google Analytics