Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 320|回复: 0

[默认分类] C++ STL学习之stack。

[复制链接]
  • TA的每日心情
    开心
    2021-12-13 21:45
  • 签到天数: 15 天

    [LV.4]偶尔看看III

    发表于 2018-5-21 14:20:11 | 显示全部楼层 |阅读模式

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

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

    这个基础的容器可能是任何标准的容器类,和一些其他特殊设计的模板类,唯一的要求就是要支持一下的操作
    1. [b]•[/b]back()
    2. •push_back()
    3. •pop_back()
    复制代码

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

    实现C++  STL,栈有两个参数。
      
      
       
       
          
         
    1. [code]template < class T, class Container = deque<T> > class stack;
    复制代码
    [/code]  
       
       
      
      
    参数示意:

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

    成员函数
    stack::stack
    explicit stack ( const Container& ctnr = Container() );
    用于构造一个栈适配器对象。

      
       ctnr
      
      
       Container object
       

       Container is the second class template parameter (the type of the underlying container for the
       stack; by default:
       deque<T>, see
       class description).
      

    1. // test_stack.cpp : 定义控制台应用程序的入口点。
    2. //
    3. #include "stdafx.h"
    4. #include <stack>
    5. #include <vector>
    6. #include <deque>
    7. #include <iostream>
    8. using namespace std;
    9. int _tmain(int argc, _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 << "size of first: " << (int) first.size() << endl;
    18.         cout << "size of second: " << (int) second.size() << endl;
    19.         cout << "size of third: " << (int) third.size() << endl;
    20.         cout << "size of fourth: " << (int) fourth.size() << endl;
    21.         return 0;
    22. }
    复制代码

    output:

      
      
       
       
         
    1. size of first: 0
    2. size of second: 3
    3. size of third: 0
    4. size of fourth: 2
    复制代码

       
       
      
      
    stack::empty
    1. bool empty ( ) const;
    复制代码

    判断是否为空。
    Return Value
    true if the container size is 0, false otherwise.

    1. // stack::empty
    2. #include <iostream>
    3. #include <stack>
    4. using namespace std;
    5. int main ()
    6. {
    7.   stack<int> mystack;
    8.   int sum (0);
    9.   for (int i=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.   
    17.   return 0;
    18. }
    复制代码

    Output:

      
      
       
       
         
    1. total: 55
    复制代码

       
       
      
      
    stack::pop
    void pop ( );
    在栈的顶部移除元素。
       
    1. [code]
    复制代码
    [/code]
    1. // stack::push/pop
    2. #include <iostream>
    3. #include <stack>
    4. using namespace std;
    5. int main ()
    6. {
    7.   stack<int> mystack;
    8.   for (int i=0; i<5; ++i) mystack.push(i);
    9.   cout << "Popping out elements...";
    10.   while (!mystack.empty())
    11.   {
    12.      cout << " " << mystack.top();
    13.      mystack.pop();
    14.   }
    15.   cout << endl;
    16.   return 0;
    17. }
    复制代码


       
    Output:

      
      
       
       
         
    1. Popping out elements... 4 3 2 1 0
    复制代码

       
       
      
      
       
      
      
      stack::push
      
      
       
       
      
    1. void push ( const T& x );
    复制代码

       
      
    在栈顶添加元素
    1. [code]
    复制代码
    [/code]
    1. // stack::push/pop
    2. #include <iostream>
    3. #include <stack>
    4. using namespace std;
    5. int main ()
    6. {
    7.   stack<int> mystack;
    8.   for (int i=0; i<5; ++i) mystack.push(i);
    9.   cout << "Popping out elements...";
    10.   while (!mystack.empty())
    11.   {
    12.      cout << " " << mystack.top();
    13.      mystack.pop();
    14.   }
    15.   cout << endl;
    16.   return 0;
    17. }
    复制代码


    Output:

      
      
       
       
         
    1. Popping out elements... 4 3 2 1 0
    复制代码

       
       
      
      
      
      stack::size
      
       
       
      
      
       
       
      
    1. size_type size ( ) const;
    复制代码

       
      
    计算栈对象元素个数
       

      
       
       
    1. [code]
    复制代码
    [/code]
    1. // stack::size
    2. #include <iostream>
    3. #include <stack>
    4. using namespace std;
    5. int main ()
    6. {
    7.   stack<int> myints;
    8.   cout << "0. size: " << (int) myints.size() << endl;
    9.   for (int i=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.   return 0;
    14. }
    复制代码
      
       
      



    Output:

      
      
       
       
         
    1. 0. size: 0
    2. 1. size: 5
    3. 2. size: 4
    复制代码

       
       
      
      
      
      stack::top
      
       
       
      
      
       
       
      
    1.       value_type& top ( );
    2. 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. using namespace std;
    9. int _tmain(int argc, _TCHAR* argv[])
    10. {
    11.         stack<int> mystack;
    12.         mystack.push(10);
    13.         mystack.push(20);
    14.         mystack.top()-=5;
    15.         cout << "mystack.top() is now " << mystack.top() << endl;
    16.         return 0;
    17. }
    复制代码

    Output:

      
      
       
       
         
    1. mystack.top() is now 15
    复制代码

       
       
      
      
       
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-5-16 16:30 , Processed in 0.417046 second(s), 37 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表