C++基于阵列的堆栈
C++ Array Based Stack
一旦堆栈已满,我正在尝试将堆栈加倍。我尝试调用复制构造函数,当我调用它时,堆栈不会继续推送单词。代码在满载之前完美运行,但一旦填满就是我出现问题的地方。我做错了什么?
#include<iostream>
#include<fstream>
#include<string>
//#include "ArgumentManager.h"
#include "Stack.h"
using namespace std;
int main(int argc, char** argv){
//ArgumentManager am(argc, argv); //Instantiating parser for command line arguments
//ReAdInG tHe InPuT fIlE nAmE
//ifstream infile(am.get("A").c_str()); // file to read from, getting name from command line
//ofstream outfile(am.get("C").c_str()); // output file, getting name from command line
ifstream infile;
ofstream outfile;
arrayStack<string> firstStack(10);
arrayStack<string> secondStack(firstStack);
firstStack.stackInitialize();
infile.open("input.txt");
outfile.open("output.txt");
//iF tHe FiLe IsNt ReAd
if(!infile){
std::cout << "ErRor[Input file not Found] YoU hAd OnE jOb....ShAmE" << endl;
std::cout << "ExItInG PrOgRaM!!! GoOdByE" << endl;
return 0;
};
string tester; // to get the words
//READ FROM INFILE AND OUTPUT TO OUTFILE
while(infile >> tester){
for(int i = 0; i < tester.size(); ++i)
{ // CHECK IF A SPECIAL CHARACTER IS ON THE FILE
if(!((tester[i] >= 'a' && tester[i] <= 'z')||(tester[i] >= 'A' && tester[i]<= 'Z')))
{
tester[i] = ' ';
}
}
firstStack.push(tester);
};
while(!firstStack.stackIsEmpty())
{
string b = firstStack.top();
outfile << b << " ";
cout << b << " ";
if(firstStack.stackIsFull()){
secondStack.push(tester)
};
firstStack.pop();
}
infile.close();
outfile.close();
return 0;
}
我还尝试在推送函数中调用复制构造函数,例如:
template <class Type>
void arrayStack<Type>::push(const Type& word){
if(topStack != maxStackSize){
list[topStack] = word; // adding a new word to the STACK
topStack++;
}
else
cout << "YOU KNOW YOU CAN'T ADD TO A FULL STACK............SHAME" << endl;
arrayStack<Type> newArrayStack(maxStackSize*2);
for(int i = 0; i < maxStackSize; i++){
newArrayStack.push(list[i]);
}
newArrayStack.push(word);
stackCopy(newArrayStack);
}
它也不起作用。
下面是堆栈模板
//ARRAY BASED STACK TEMPLATE
#ifndef H_ArrayStack
#define H_ArrayStack
#include <iostream>
using namespace std;
template <class Type>
class arrayStack{
private:
int maxStackSize; // the maximum height of the STACK
int topStack; // the top of the STACK
void stackCopy(const arrayStack<Type>& newArrayStack);
Type *list; // array based needs pointer to hold the stack element
public:
const arrayStack<Type>& operator=(const arrayStack<Type>&);
void stackInitialize(){ topStack = 0;}; //Ensure the array stack is empty
bool stackIsEmpty() const{return(topStack == 0);}; //check if stack is empty, is const so will not be messed with
bool stackIsFull() const{return topStack == maxStackSize;}; // just like line 8 except check if it is full
void push(const Type& word); // add a word to the array STACK
void pop(); //remove a word from the array and increment the top
Type top() const; //returns the top of the STACK
arrayStack(int size); //the default constructor
arrayStack(const arrayStack<Type>& newArrayStack); // the copy constructor which allows a new STACK
~arrayStack(){delete [] list;}; // it is an array so to ensure no memory leaks the stack must be deleted after use
};
template <class Type>
void arrayStack<Type>::push(const Type& word){
if(topStack != maxStackSize){
list[topStack] = word; // adding a new word to the STACK
topStack++;
}
else{
cout << "YOU KNOW YOU CAN'T ADD TO A FULL STACK............SHAME" << endl;
int size = maxStackSize;
maxStackSize *= 2;
Type *temp = new Type[maxStackSize]; // create temp and double the size of list
for(int i = 0; i < size; i++){ // copy over all the values
temp[i] = list[i];
}
delete [] list; // delete the list
list = temp; // point to the resized list
list[topStack] = word;
topStack++;
}
}
template <class Type>
void arrayStack<Type>::pop(){
if (!stackIsEmpty()){
topStack--;
}
}
template <class Type>
Type arrayStack<Type>::top() const{
if(topStack == 0){
return 0;
}
else
return list[topStack - 1];
}
template <class Type>
arrayStack<Type>::arrayStack(int size){
maxStackSize = size;
topStack = 0;
list = new Type[maxStackSize];
}
template <class Type>
void arrayStack<Type>::stackCopy(const arrayStack<Type>& newArrayStack){
maxStackSize = newArrayStack.maxStackSize;
topStack = newArrayStack.topStack;
list = new Type[maxStackSize];
for(int j = 0; j < topStack; j++)
list[j] = newArrayStack.list[j];
}
template <class Type>
arrayStack<Type>::arrayStack(const arrayStack<Type>& newArrayStack){
stackCopy(newArrayStack);
}
template <class Type>
const arrayStack<Type>& arrayStack<Type>::operator=(const arrayStack<Type>& newArrayStack){
if(this != &newArrayStack)
stackCopy(newArrayStack);
return *this;
}
#endif
来自:
arrayStack<Type> newArrayStack(maxStackSize*2);
for(int i = 0; i < maxStackSize; i++){
newArrayStack.push(list[i]);
}
newArrayStack.push(word);
stackCopy(newArrayStack);
自:
int size = maxStackSize;
maxStackSize *= 2;
Type *temp = new Type[maxStackSize]; // create temp and double the size of list
for(int i = 0; i < size; i++){ // copy over all the values
temp[i] = list[i];
}
delete [] list; // delete the list
list = temp; // point to the resized list
list[topStack] = word;
topStackk++;
在推送函数中尝试此操作,这将使堆栈的大小增加一倍。就像评论中提到的某人一样,您的复制构造函数和堆栈复制函数中有错误
相关文章:
- OpenMP阵列性能较差
- 如何将三维尺寸不固定的三维阵列展平为一维阵列
- 当我的阵列太大时出现分段错误
- 算法问题:查找从堆栈中弹出的所有序列
- 使用模板进行堆栈实现; "name followed by :: must be a class or namespace"
- Visual Studio(或任何其他工具)能否将地址解释为调用堆栈(boost上下文)的开头
- 我可以在堆栈上分配`std :: array`阵列,而不知道编译时大小
- 通过"Value Template Argument"与常规阵列在堆栈中分配内存
- 加载到阵列中会导致堆栈粉碎,同时有足够的空间
- 安全分配堆栈分配的阵列
- sizeof()在堆栈上的阵列与堆上的尺寸
- C++基于阵列的堆栈
- 为什么我会看到在堆和堆栈上分配的阵列之间的不同行为
- 全局阵列分配 -- 堆栈或堆
- 使用小型2D阵列会导致堆栈溢出(main之前的seg错误)
- 阵列和阵列堆栈之间的区别
- C++堆栈和2D阵列
- 阵列周围的堆栈已损坏C++
- 2D阵列100x100堆栈溢出;堆上没有错误.c++
- 堆阵列分配而不是堆栈上的堆数组分配