这里是我的执行pop
。从这个例子中可以明显看出push
需要做什么。 我不能说这是最具成本效益的方法。
template <class T>
T SimpleStack<T>::popOff()
{
T popped = *(aptr + --arraySize); //aptr points to the existing stack
int tSize = arraySize; //arraySize is a member holding the size
T *temp = new T[tSize]; //Temp holder for the elements that stay
//on the stack
for(int i = 0; i < tSize; ++i)
{
*(temp+i) = *(aptr+i); //Fill the temp holder with the original
//stack - popped
}
delete [] aptr; //Get rid of the old stack
aptr = new T [tSize]; //Create a new stack with the new size
for(int i = 0; i < arraySize; ++i)
{
*(aptr+i) = *(temp+i); //Fill the new stack with the kept values
}
delete [] temp; //Get rid of the temp holder
return popped; //Send the popped number back
}
事实依然,没有什么堆栈,或者你想模仿任何自定义的容器,是和如何使用它,并在那里的最适合你可能会挣扎读了。
在C++中没有“指针推送和弹出”。另外'NULL'并不意味着“空”。你能分享你的代码,以便我们可以帮助你解决它吗?我还建议阅读[tag:stack] wiki页面以更好地理解堆栈是什么以及它是如何工作的。 – Johnsyweb 2013-02-09 23:00:35
如果你不知道你已经知道什么以及你不知道什么,这真的很难帮助你。例如,你是否理解[std :: stack](http://en.cppreference.com/w/cpp/container/stack )是?如果不是,你通常理解容器吗? – 2013-02-09 23:01:36
跟踪堆栈的数量或堆栈上的号码数量? – ChiefTwoPencils 2013-02-09 23:02:46