栈的特点时,先进后出

可以基于前面的向量实现

Stack.h

#include "Vector.hpp"

template <typename T>
class Stack: public Vector<T> {  //将向量的首/末端作为栈底/顶
public:
    void push(T const& e);
    
    T pop();
    
    T& top();
};


template <typename T>
void Stack<T>::push(T const& e) {
    //模版类子类使用模版类父类成员需要使用域作用符
    this->insert(this->size(), e);
}

template <typename T>
T Stack<T>::pop() {
    return this->remove(this->size() - 1);
}

template <typename T>
T& Stack<T>::top() {
    return (*this)[this->size() - 1];
}

 

posted on 2018-11-11 21:41  迷心迷  阅读(113)  评论(0编辑  收藏  举报