zoukankan      html  css  js  c++  java
  • STL之stack栈

    概述

    栈(statck)这种数据结构在计算机中是相当出名的。

    栈中的数据是先进后出的(First In Last Out, FILO)。栈只有一个出口,允许新增元素(只能在栈顶上增加)、移出元素(只能移出栈顶元素)、取得栈顶元素等操作。

    在STL中,栈是以别的容器作为底部结构,再将接口改变,使之符合栈的特性就可以了。因此实现非常的方便。

    namespace std {
           template <class T,
                     class Container = deque<T> >
           class stack;
       }

      

      The first template parameter is the type of the elements. The optional second template parameter defines the container that is used internally by the queue for its elements. The default container is a deque. It was chosen because, unlike vectors, deques free their memory when elements are removed and don't have to copy all elements on reallocation .

    操作

    The core interface of stacks is provided by the member functions push(), top(), and pop():

    • push() inserts an element into the stack.

    • top() returns the next element in the stack.

    • pop() removes an element from the stack.

    • stack<ElementType> c create a empty stack.
    • stack<ElementType> c1(c2) copy a new stack from c2.
    • empty() return wheather the stack is empty.
    • size() return the number of element in the stack.

    实现源代码

    namespace std {
          template <class T, class Container = deque<T> >
          class stack {
            public:
              typedef typename Container::value_type value_type;
              typedef typename Container::size_type  size_type;
              typedef          Container             container_type;
              protected:
                Container c;     // container
              public:
                explicit stack(const Container& = Container());
    
                bool        empty() const             { return c.empty(); }
                size_type   size()    const           { return c.size(); }
                void push   (const value_type& x)     { c.push_back(x); }
                void        pop()                     { c.pop_back(); }
                value_type& top()                     { return c.back(); }
                const value_type& top() const         { return c.back(); }
          };
    
          template <class T, class Container>
            bool operator==(const stack<T, Container>&,
                            const stack<T, Container>&);
          template <class T, class Container>
            bool operator< (const stack<T, Container>&,
                            const stack<T, Container>&);
          ...// (other comparison operators)
       }

      

      从实现源代码中可以看出,由于栈只是进一步封装别的数据结构,并提供自己的接口,所以代码非常简洁,如果不指定容器,默认是用deque来作为其底层数据结构的(对deque不是很了解?可以参阅《STL之deque双向队列》)。

    使用范例

    下面给出栈的使用范例:

    // cont/stack1.cpp
    
       #include <iostream>
       #include <stack>
       using namespace std;
    
       int main()
       {
    
           stack<int> st;
    
           // push three elements into the stack
           st.push(l);
           st.push(2);
           st.push(3);
    
           // pop and print two elements from the stack
           cout << st.top() << ' ';
           st.pop() ;
           cout << st.top() << ' ';
           st.pop() ;
    
           // modify top element
           st.top() = 77;
    
           // push two new elements
           st.push(4);
           st.push(5);
    
           // pop one element without processing it
           st.pop() ;
    
           // pop and print remaining elements
           while (!st.empty()) {
               cout << st.top() << ' ';
               st.pop() ;
           }
           cout << endl;
       }

    The output of the program is as follows:

    					
       3  2  4  77
    
  • 相关阅读:
    Eagle+微力同步实现素材资源协同共享
    Eagle+欧奥PicHome创建私有的pinterest网站
    C# 从Json中获取byte[] 二进制数据
    Vivado SDK 2016.4改JTAG速度
    启动jmeter,提示:unable to access jarfile apacheJmeter.jar
    直播来啦!亮相微软平台,聊聊Dapr的落地应用
    互斥锁、自旋锁、读写锁...理清它们的区别和应用
    行云创新:云原生技术助力企业数字化转型
    行云创新亮相“OSCAR开源产业大会”:云调试加速应用创新
    Actor模型是如何让编写并发系统变得更简单的?
  • 原文地址:https://www.cnblogs.com/wiessharling/p/3988461.html
Copyright © 2011-2022 走看看