[英]Template Stack and LIFO C++
因此,我试图了解模板以及Fifo和Lifo堆栈内容。 我一直在处理一些与此相关的代码,并且我可以获取int数据来做我想测试的事情,但是我一生都无法弄清楚如何使它与字符串一起工作。 我拥有代码的方式不断使我崩溃,但是没有给我任何错误,所以我认为我会在这里弹出,看看是否有人可以告诉我我在做什么错。 这是我的代码:
-----------//my header//---------------------
#include <stdlib.h>
#include <iostream>
#include <string>
#ifndef STACK_H_
#define STACK_H_
template<class T>
class StackTest
{
private:
unsigned int maxSize;
T *stackData;
int top;
public:
StackTest(int size){
stackData = new T[size];//to hold the T type data items
top = -1;//no items on the stack
maxSize = size;//set maximum size that stack can hold
}
virtual ~StackTest(){}
int count(){
return top + 1;
}
bool isEmpty(){
return top == -1 ? true : false;
}
bool isFull(){
return top == maxSize - 1 ? true : false;
}
T* peek(){
if(!isEmpty())//check for empty
return &stackData[top - 1];
}
T* pop(){
if(!isEmpty()){
top -= 1;//decrease the top by 1 to indicate the delete
return &stackData[top];//return deleted item
}
return NULL;
}
void push(T* item){
stackData[top++] = *item;//insert to data array and increase the top by one
}
};
#endif /* STACK_H_ */
-----------//my main//---------------
#include <iostream>
#include <string>
#include "Pair.h"
using namespace std;
int main() {
int dataTest;
string strTest;
StackTest<int> intStack(10);
StackTest<string> stringStack(50);
//Insert data into the stack
dataTest = 3;
intStack.push(&dataTest);
dataTest = 4;
intStack.push(&dataTest);
dataTest = 5;
intStack.push(&dataTest);
dataTest = 6;
intStack.push(&dataTest);
strTest = "test";
stringStack.push(&strTest);
//Show the top item
cout << *intStack.peek() << endl;
cout << *stringStack.peek() << endl;
//Pull the top item out (twice)
intStack.pop();
intStack.pop();
//Show the new top item
cout << *intStack.peek() << endl;
return 0;
}
因此,如果有人想给我一些指导,我将非常感激,谢谢。
您的实现存在一些问题。 最微妙的之一是push()
成员函数:
void push(T* item){
stackData[top++] = *item; //insert to data array and increase the top by one
// ^^
// You want pre-increment here!
}
这将递增top
并使用旧值作为stackData
的索引。 由于堆栈为空时top
为-1
,因此您的程序实际上正在执行:
stackData[-1] = *item;
top = 0;
不用说,第一次分配会导致未定义的行为。
不确定行为的另一个来源是peek()
成员函数,该函数在堆栈为空时不返回任何内容:
T* peek(){
if(!isEmpty())//check for empty
return &stackData[top - 1];
}
根据C ++ 11标准的6.6.3 / 2段:
[...]从函数的结尾流出就等于没有值的返回; 这导致返回值函数中的行为不确定。
但这不是唯一的问题:另一个问题是访问stackData
:
return &stackData[top - 1];
当top
不等于或大于1时,这也会导致未定义的行为,因为您将获取位于数组负地址中的(非)对象的地址。
另外,我建议重写isEmpty()
和isFull()
,如下所示:
bool isEmpty(){
return (top == -1);
}
bool isFull(){
return (top == maxSize - 1);
}
作为一般建议,请考虑在堆栈为空时不要将值-1
用于top
。 正如Ben Voigt在评论中提到的那样 ,这会导致您出现许多不一的错误。
另外, 正如DyP所指出的那样 ,析构函数不会释放在构造函数中分配的内存,因此您的StackTest
对象正在泄漏内存。 然后,既然如此,您可能想要看一看所谓的“三规则” ,即您的程序将被违反。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.