zoukankan      html  css  js  c++  java
  • Stack实现方式

    Stack的一种实现

    /*
     * stack实现:利用vector
     
    */
    #include <iostream>
    #include <vector>
    #include <string>

    using std::cout;
    using std::endl;
    using std::vector;
    using std::string;
    using std::ostream;

    template <class T>
    class Stack{
        public:
            Stack(int cap=0){
                if(cap)
                    _stack.reserve(cap);
            }
            bool pop(T &vaulue);
            bool push(T  value);
            bool full();
            bool empty();
            void display();
            int size();
        private:
            vector<T> _stack;
    };
    template<class T>
    inline int Stack<T>::size(){
        return _stack.size();
    }
    template<class T>
    inline bool Stack<T>::empty(){
        return _stack.empty();
    }
    template<class T>
    inline bool Stack<T>::full(){
        return _stack.max_size()==_stack.size();
    }
    template<class T>
    bool Stack<T>::pop(T &value){
        if(empty())
            return false;
        value=_stack.back();
        _stack.pop_back();
        cout<<"Stack:pop()"<<value<<endl;
        return true;
    }
    template<class T>
    bool Stack<T>::push(T value){
        if(full())
            return false;
        _stack.push_back(value);
        cout<<"Stack:push()"<<value<<endl;
        return true;

    }
    template<class T>
    void Stack<T>::display(){
        if(size()==0) { cout<<"(0)"<<endl;
        }
        else{
            cout<<"("<<size()<<")(bot:";
            for(int i=0;i<size();++i)
                cout<<_stack[i]<<" ";
            cout<<":top)"<<endl;
        }
    }
    class MyTest{
        friend ostream& operator<<(ostream& os,MyTest& mt){
            os<<mt.str;
            return os;
        }

        public:
            MyTest(string s=""):str(s){
                cout<<"constructor"<<endl;
            };
        private:
            string str;
    };
    int main()
    {
    //    Stack<int> stack( 32 );
    //    stack.display();
    //    for ( int ix = 1; ix < 51; ++ix )
    //    {
    //        if ( ix%2 == 0 )
    //            stack.push( ix );
    //        if ( ix%5 == 0 )
    //            stack.display();
    //        if ( ix%10 == 0) {
    //            int dummy;
    //            stack.pop( dummy ); stack.pop( dummy );
    //            stack.display();
    //        }
    //    }
         Stack<MyTest> stack(10);
         stack.display();
         stack.push(MyTest("hello"));
         stack.display();
        return 0;
    }
  • 相关阅读:
    我的2019,探索在路上
    深度学习-TensorFlow2.0笔记(一)
    机器学习笔记(二)
    机器学习笔记(一)
    SQL注入分类以及联合查询回显注入
    数据库基础概念
    SQL注入原理、常见思路之Access手工注入实战
    OWASP top 10 2017
    HTTP协议及burp基本使用
    web系统架构
  • 原文地址:https://www.cnblogs.com/xkfz007/p/2653812.html
Copyright © 2011-2022 走看看