我正在尝试使用一组固定长度的子堆栈来实现堆栈。对于子堆栈,我使用在构造函数中初始化的固定长度的简单数组。对于堆栈,我使用子堆栈向量。
下面的push方法尝试将内存分配给新的子包并将其推送到向量。但是每当发生新的推送时(让我们在向量setofstacks [1]上说),之前的setofstacks [0]会被调用的substack析构函数获取垃圾值。不知道为什么会发生这种情况。
substack<T> *s = new substack<T>(size);
setofstacks.push_back(*s);
setofstacks[currStackIndex].push(elem);
vector<substack<T>*> setofstacks;
另一方面,如果我将对象转换为子包的对象指针并将其存储在向量中,那么效果非常好。
setofstacks.push_back(new substack<T>(size));
setofstacks[currStackIndex]->push(elem);
/* Program: Implement a stack of substacks of fixed size
*
* Date: 12/28/2015
*/
#include <iostream>
#include <vector>
using namespace std;
template <class T>
class substack
{
private:
T *arr;
int nextAvailable;
int size;
public:
substack(int capacity);
~substack();
void push(T elem);
T pop();
T peek();
bool isfull();
bool isempty();
};
// Constructor initializing size of substack
template <class T>
substack<T>::substack(int capacity)
{
nextAvailable = 0;
size = capacity;
arr = new T[capacity];
}
// Destructor freeing memory allocated by the stack implemented using array
template <class T>
substack<T>::~substack()
{
delete[] arr;
arr = NULL;
}
// Pushing an element on top of a substack of fixed size
template <class T>
void substack<T>::push(T elem)
{
if (nextAvailable < size)
{
arr[nextAvailable] = elem;
nextAvailable++;
}
else
{
cout << "Substack is full" << endl;
return;
}
}
// Popping the top element from a stack of fixed size
template <class T>
T substack<T>::pop()
{
nextAvailable--;
if (nextAvailable >= 0)
{
T del = arr[nextAvailable];
arr[nextAvailable] = NULL;
return del;
}
else
{
cout << "Stack is empty" << endl;
return NULL;
}
}
// Peeking the top element from a stack of fixed size
template <class T>
T substack<T>::peek()
{
if (nextAvailable > 0)
{
return arr[nextAvailable - 1];
}
else
{
cout << "Stack is empty" << endl;
return NULL;
}
}
// Checking if a substack is full or not
template <class T>
bool substack<T>::isfull()
{
return (nextAvailable == size);
}
// Checking if a substack is empty or not
template <class T>
bool substack<T>::isempty()
{
return (nextAvailable == 0);
}
template <class T>
class SetOfStacks
{
private:
//vector<substack<T>*> setofstacks;
vector<substack<T>> setofstacks;
int currStackIndex;
int size;
public:
SetOfStacks(int capacity);
~SetOfStacks();
void push(T elem);
T pop();
T peek();
};
// Constructor initializing a set of stacks
template <class T>
SetOfStacks<T>::SetOfStacks(int maxStackCapacity)
{
currStackIndex = 0;
size = maxStackCapacity;
substack<T> *s = new substack<T>(maxStackCapacity);
setofstacks.push_back(*s);
//setofstacks.push_back(new substack<T>(maxStackCapacity));
}
// Destructor clearing set of stacks vector
template <class T>
SetOfStacks<T>::~SetOfStacks()
{
setofstacks.clear();
}
// Pushing an element on top of a stack implemented by a set of fixed length substacks
template <class T>
void SetOfStacks<T>::push(T elem)
{
if (!setofstacks[currStackIndex].isfull())
{
setofstacks[currStackIndex].push(elem);
}
else
{
currStackIndex++;
substack<T> *s = new substack<T>(size);
setofstacks.push_back(*s);
//setofstacks.push_back(new substack<T>(size));
setofstacks[currStackIndex].push(elem);
}
}
// Popping an element from top of a stack implemented by a set of fixed length substacks
template <class T>
T SetOfStacks<T>::pop()
{
if (!setofstacks[currStackIndex].isempty())
{
return setofstacks[currStackIndex].pop();
}
else
{
setofstacks.pop_back();
currStackIndex--;
if (currStackIndex >= 0)
{
return setofstacks[currStackIndex].pop();
}
else
{
cout << "Stack is empty" << endl;
return NULL;
}
}
}
// Peeking an element from top of a stack implemented by a set of fixed length substacks
template <class T>
T SetOfStacks<T>::peek()
{
return setofstacks[currStackIndex].peek();
}
int main()
{
SetOfStacks<int> *s = new SetOfStacks<int>(5);
s->push(1);
s->push(2);
s->push(3);
s->push(4);
s->push(5);
s->push(6);
s->push(7);
s->push(8);
cout << s->pop() << endl;
cout << s->pop() << endl;
cout << s->pop() << endl;
cout << s->pop() << endl;
cout << s->pop() << endl;
cout << s->pop() << endl;
cout << s->pop() << endl;
cout << s->pop() << endl;
s->push(1);
s->push(2);
s->push(3);
cout << s->pop() << endl;
cout << s->pop() << endl;
cout << s->pop() << endl;
//cout << s->pop() << endl;
system("pause");
}
无法使用完整代码
8
7
6
-572662307
-572662307
-572662307
-572662307
-572662307
3
2
1
Press any key to continue . . .
输出
/* Program: Implement a stack of substacks of fixed size
*
* Date: 12/28/2015
*/
#include <iostream>
#include <vector>
using namespace std;
template <class T>
class substack
{
private:
T *arr;
int nextAvailable;
int size;
public:
substack(int capacity);
~substack();
void push(T elem);
T pop();
T peek();
bool isfull();
bool isempty();
};
// Constructor initializing size of substack
template <class T>
substack<T>::substack(int capacity)
{
nextAvailable = 0;
size = capacity;
arr = new T[capacity];
}
// Destructor freeing memory allocated by the stack implemented using array
template <class T>
substack<T>::~substack()
{
delete[] arr;
arr = NULL;
}
// Pushing an element on top of a substack of fixed size
template <class T>
void substack<T>::push(T elem)
{
if (nextAvailable < size)
{
arr[nextAvailable] = elem;
nextAvailable++;
}
else
{
cout << "Substack is full" << endl;
return;
}
}
// Popping the top element from a stack of fixed size
template <class T>
T substack<T>::pop()
{
nextAvailable--;
if (nextAvailable >= 0)
{
T del = arr[nextAvailable];
arr[nextAvailable] = NULL;
return del;
}
else
{
cout << "Stack is empty" << endl;
return NULL;
}
}
// Peeking the top element from a stack of fixed size
template <class T>
T substack<T>::peek()
{
if (nextAvailable > 0)
{
return arr[nextAvailable - 1];
}
else
{
cout << "Stack is empty" << endl;
return NULL;
}
}
// Checking if a substack is full or not
template <class T>
bool substack<T>::isfull()
{
return (nextAvailable == size);
}
// Checking if a substack is empty or not
template <class T>
bool substack<T>::isempty()
{
return (nextAvailable == 0);
}
template <class T>
class SetOfStacks
{
private:
vector<substack<T>*> setofstacks;
int currStackIndex;
int size;
public:
SetOfStacks(int capacity);
~SetOfStacks();
void push(T elem);
T pop();
T peek();
};
// Constructor initializing a set of stacks
template <class T>
SetOfStacks<T>::SetOfStacks(int maxStackCapacity)
{
currStackIndex = 0;
size = maxStackCapacity;
//substack<T> *s = new substack<T>(maxStackCapacity);
setofstacks.push_back(new substack<T>(maxStackCapacity));
}
// Destructor clearing set of stacks vector
template <class T>
SetOfStacks<T>::~SetOfStacks()
{
setofstacks.clear();
}
// Pushing an element on top of a stack implemented by a set of fixed length substacks
template <class T>
void SetOfStacks<T>::push(T elem)
{
if (!setofstacks[currStackIndex]->isfull())
{
setofstacks[currStackIndex]->push(elem);
}
else
{
currStackIndex++;
//substack<T> *s = new substack<T>(size);
setofstacks.push_back(new substack<T>(size));
setofstacks[currStackIndex]->push(elem);
}
}
// Popping an element from top of a stack implemented by a set of fixed length substacks
template <class T>
T SetOfStacks<T>::pop()
{
if (!setofstacks[currStackIndex]->isempty())
{
return setofstacks[currStackIndex]->pop();
}
else
{
setofstacks.pop_back();
currStackIndex--;
if (currStackIndex >= 0)
{
return setofstacks[currStackIndex]->pop();
}
else
{
cout << "Stack is empty" << endl;
return NULL;
}
}
}
// Peeking an element from top of a stack implemented by a set of fixed length substacks
template <class T>
T SetOfStacks<T>::peek()
{
return setofstacks[currStackIndex].peek();
}
int main()
{
SetOfStacks<int> *s = new SetOfStacks<int>(5);
s->push(1);
s->push(2);
s->push(3);
s->push(4);
s->push(5);
s->push(6);
s->push(7);
s->push(8);
cout << s->pop() << endl;
cout << s->pop() << endl;
cout << s->pop() << endl;
cout << s->pop() << endl;
cout << s->pop() << endl;
cout << s->pop() << endl;
cout << s->pop() << endl;
cout << s->pop() << endl;
s->push(1);
s->push(2);
s->push(3);
cout << s->pop() << endl;
cout << s->pop() << endl;
cout << s->pop() << endl;
cout << s->pop() << endl;
system("pause");
}
工作代码
8
7
6
5
4
3
2
1
3
2
1
Stack is empty
0
Press any key to continue . . .
输出
cvs update -r "1.5"
请在添加实际的substack对象时让我知道我在使用push_back时缺少什么,这样它就不会弄乱向量中的先前索引子包。
不确定这是如何成为三人统治的骗局。我错过了三个复制构造函数,赋值运算符之一吗?请帮我理解。