我试图创建自己的类(NodeWithMin)作为C++中的堆栈元素,并创建一个继承它的新类(StackWithMin).我想我可以创建新的堆栈类,但是有一些问题初始化新类的新实例并使用它.有没有人对它有好主意?我在一个文件中写了所有类和main.谢谢.
#include <stack>
class NodeWithMin{
public:
int value;
int min;
NodeWithMin(int v, int min){
this->value = v;
this->min = min;
}
};
template<class NodeWithMin>
class StackWithMin : stack<NodeWithMin>{
public:
typedef stack<NodeWithMin> super;
void push(int value){
int newMin = min(value, this->min());
super::push(new NodeWithMin(value, newMin));
};
int min(){
if(this->isEmpty()){
return numeric_limits<int>::max();
}else{
super::peek().min;
}
};
};
int main(int argc, const char * argv[])
{
StackWithMin<class NodeWithMin>* ss;
ss = new StackWithMin<class NodeWithMin>();
}
Run Code Online (Sandbox Code Playgroud)