zoukankan      html  css  js  c++  java
  • 10-stack

    c++ stlstack介绍

    C++ Stack(堆栈) 是一个容器的改编,为程序员提供了堆栈的全部功能,——也就是说实现了一个先进后出(FILO)的数据结构。

    c++ stl栈stack的头文件

    #include <stack> 

    c++ stl栈stack的成员函数介绍

    操作 比较和分配堆栈

    empty() 堆栈为空则返回真

    pop() 移除栈顶元素

    push() 在栈顶增加元素

    size() 返回栈中元素数目

    top() 返回栈顶元素

    stack 介绍

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

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

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

    [cpp] view plain copy
     
    1. <span style="font-size:16px;"><strong>•</strong>back()   
    2. •push_back()   
    3. •pop_back()</span>   

    因此,标准的容器类模板vectordeque 和list可以使用,默认情况下,如果没有容器类被指定成为一个提别的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
    Container is the second class template parameter (the type of the underlying container for thestack; by default: deque<T>, see class description).
    [cpp] view plain copy
     
    1. // test_stack.cpp : 定义控制台应用程序的入口点。  
    2. //  
    3.   
    4. #include "stdafx.h"  
    5. #include <stack>  
    6. #include <vector>  
    7. #include <deque>  
    8. #include <iostream>  
    9.   
    10. using namespace std;  
    11.   
    12. int _tmain(int argc, _TCHAR* argv[])  
    13. {  
    14.     deque<int> mydeque(2,100);  
    15.     vector<int> myvector(2,200);  
    16.   
    17.     stack<int> first;  
    18.     stack<int> second(mydeque);  
    19.   
    20.     stack<int,vector<int> > third;  
    21.     stack<int,vector<int> > fourth(myvector);  
    22.   
    23.     cout << "size of first: " << (int) first.size() << endl;  
    24.     cout << "size of second: " << (int) second.size() << endl;  
    25.     cout << "size of third: " << (int) third.size() << endl;  
    26.     cout << "size of fourth: " << (int) fourth.size() << endl;  
    27.   
    28.   
    29.     return 0;  
    30. }  

    output:

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

    stack::empty

    bool empty ( ) const;

    判断是否为空。

    Return Value

    true if the container size is 0false otherwise.

    [cpp] view plain copy
     
    1. // stack::empty  
    2. #include <iostream>  
    3. #include <stack>  
    4. using namespace std;  
    5.   
    6. int main ()  
    7. {  
    8.   stack<int> mystack;  
    9.   int sum (0);  
    10.   
    11.   for (int i=1;i<=10;i++) mystack.push(i);  
    12.   
    13.   while (!mystack.empty())  
    14.   {  
    15.      sum += mystack.top();  
    16.      mystack.pop();  
    17.   }  
    18.   
    19.   cout << "total: " << sum << endl;  
    20.     
    21.   return 0;  
    22. }  

    Output:

    total: 55
    

    stack::pop

    void pop ( );

    在栈的顶部移除元素。

    [cpp] view plain copy
     
    1. // stack::push/pop  
    2. #include <iostream>  
    3. #include <stack>  
    4. using namespace std;  
    5.   
    6. int main ()  
    7. {  
    8.   stack<int> mystack;  
    9.   
    10.   for (int i=0; i<5; ++i) mystack.push(i);  
    11.   
    12.   cout << "Popping out elements...";  
    13.   while (!mystack.empty())  
    14.   {  
    15.      cout << " " << mystack.top();  
    16.      mystack.pop();  
    17.   }  
    18.   cout << endl;  
    19.   
    20.   return 0;  
    21. }  

     

    Output:

    Popping out elements... 4 3 2 1 0
    

    stack::push

     
    void push ( const T& x );

    在栈顶添加元素

    [cpp] view plain copy
     
    1. // stack::push/pop  
    2. #include <iostream>  
    3. #include <stack>  
    4. using namespace std;  
    5.   
    6. int main ()  
    7. {  
    8.   stack<int> mystack;  
    9.   
    10.   for (int i=0; i<5; ++i) mystack.push(i);  
    11.   
    12.   cout << "Popping out elements...";  
    13.   while (!mystack.empty())  
    14.   {  
    15.      cout << " " << mystack.top();  
    16.      mystack.pop();  
    17.   }  
    18.   cout << endl;  
    19.   
    20.   return 0;  
    21. }  

     

    Output:

    Popping out elements... 4 3 2 1 0
    

    stack::size

     
     
    size_type size ( ) const;

    计算栈对象元素个数

    [cpp] view plain copy
     
    1. // stack::size  
    2. #include <iostream>  
    3. #include <stack>  
    4. using namespace std;  
    5.   
    6. int main ()  
    7. {  
    8.   stack<int> myints;  
    9.   cout << "0. size: " << (int) myints.size() << endl;  
    10.   
    11.   for (int i=0; i<5; i++) myints.push(i);  
    12.   cout << "1. size: " << (int) myints.size() << endl;  
    13.   
    14.   myints.pop();  
    15.   cout << "2. size: " << (int) myints.size() << endl;  
    16.   
    17.   return 0;  
    18. }  



    Output:

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

    stack::top

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

    返回栈顶元素

    [cpp] view plain copy
     
    1. // test_stack.cpp : 定义控制台应用程序的入口点。  
    2. //  
    3.   
    4. #include "stdafx.h"  
    5. #include <stack>  
    6. #include <vector>  
    7. #include <deque>  
    8. #include <iostream>  
    9.   
    10. using namespace std;  
    11.   
    12. int _tmain(int argc, _TCHAR* argv[])  
    13. {  
    14.     stack<int> mystack;  
    15.     mystack.push(10);  
    16.     mystack.push(20);  
    17.     mystack.top()-=5;  
    18.     cout << "mystack.top() is now " << mystack.top() << endl;  
    19.   
    20.     return 0;  
    21. }  

    Output:

    mystack.top() is now 15
  • 相关阅读:
    Unity 摄像机Clear Flags和Culling Mask属性用途详解
    Unity 坐标系
    Unity 模型导入导出
    Unity 序列化
    正确理解静态Static关键字
    Unity 中的协同程序
    Asp.Net中调用存储过程并返回输出参数
    php学习知识点
    Jauery 中Ajax的几种异步请求
    2014年12月21号面试
  • 原文地址:https://www.cnblogs.com/zhumengdexiaobai/p/8444275.html
Copyright © 2011-2022 走看看