如何使我的堆栈类动态
how to make my stack class dynamic
我用c++写了一个堆栈类(如下所示),但它是静态的,并且肯定会使用大量内存。如何使其动态,以便在需要时为对象添加内存,在弹出内容时自动删除内存?
template <class T>
class stack
{
private:
T value[512];
uint16_t length;
public:
stack()
{
length=0;
}
stack(T _input)
{
value[0]=_input;
length=1;
}
bool push(T _input)
{
if(length+1<512)
{
value[++length]=_input;
return true;
}
else
return false;
}
T pop()
{
return value[length--];
}
T peak()
{
return value[length];
}
bool has_data()
{
return (length>0?true:false);
}
};
您必须在需要时动态地分配它。比如:
#define STACK_INITIAL_ALLOC 32
#define STACK_CHUNK_ALLOC 32
template<typename T>
class Stack
{
public:
Stack()
: data(0), entries(0), allocated(0)
{ }
Stack(const T &value)
: data(0), entries(0), allocated(0)
{
push(input);
}
~Stack()
{
if (data)
delete [] data;
}
void push(const T &value)
{
if (entries == allocated)
allocate(); // Allocate more memory
data[entries++] = value;
}
T pop()
{
if (entries > 0)
{
shrink();
return data[--entries];
}
else
throw runtime_error("stack empty");
}
T &top()
{
if (entries > 0)
return data[entries - 1];
else
throw runtime_error("stack empty");
}
// Return the number of entries in the stack
size_t count() const
{
return entries;
}
private:
T *data; // The actual stack
size_t entries; // Number of entries in stack
size_t allocated; // Allocated entries in stack
void copy(T *from, T *to)
{
for (size_t i = 0; i < entries; i++)
*to++ = *from++
}
void allocate()
{
if (data == 0)
{
allocated = STACK_INITIAL_ALLOC;
data = new T[allocated];
}
else
{
// We need to allocate more memory
size_t new_allocated = allocated + STACK_CHUNK_ALLOC;
T *new_data = new T[new_allocated];
// Copy from old stack to new stack
copy(data, new_data);
// Delete the old data
delete [] data;
allocated = new_allocated;
data = new_data;
}
}
// Shrink the stack, if lots of it is unused
void shrink()
{
// The limit which the number of entries must be lower than
size_t shrink_limit = allocated - STACK_CHUNK_ALLOC * 2;
// Do not shrink if we will get lower than the initial allocation (shrink_limit > STACK_INITIAL_ALLOC)
if (shrink_limit > STACK_INITIAL_ALLOC && entries < shrink_limit)
{
// We can shrink the allocated memory a little
size_t new_allocated = allocated - STACK_CHUNK_ALLOC;
T *new_data = new T[new_size];
copy(data, new_data);
delete [] data;
data = new_data;
allocated = new_allocated;
}
}
};
也是一个小小的免责声明,这段代码是直接写入浏览器的。它没有经过测试,但原则上应该有效……:)
您也可以使用std::vector:
template <class T>
class stack{
private:
std::vector<T> vec;
public:
inline void push(T arg){vec.push_back(arg);};
inline T pop(){return vec.pop_back();};
};
任何数组结构的增长和收缩都是昂贵的(T
必须是可复制构造的),并且必须移动所有现有的T
。如果你发现你在做大量的推/弹出操作,并且你需要保持较低的内存使用量,尝试在内部使用链表。它只需要被单独链接。
这是一个草图:
template <class T>
class stack
{
struct Node
{
T data;
Node* next;
};
public:
// methods
private:
Node *head;
};
现在,要在堆栈上推送一些东西,用T
构造一个Node
,将它的下一个指针设置为当前的head
,将head
设置为Node
指针。弹出包括将next
从head
中取出并将其设置为head
。
当然,你需要正确地管理内存的销毁等
编辑:啊,看来我假设你可能知道c++的基础知识是不正确的,我假设你做了,因为你正在使用模板。在这种情况下,忽略这个答案,直到你掌握了基本知识!
相关文章:
- 如何在C++中为堆栈动态创建结构?
- c++ 动态内存 堆栈中的分配
- 在堆或堆栈上分配的动态创建的字符串 - C
- 使用数组C++的动态堆栈
- 我正在尝试使用回溯来解决 N queen 问题,但在编译时它会给出运行时错误(动态堆栈缓冲区溢出)
- 为什么不能在 Visual C++ 中动态分配堆栈内存?但海湾合作委员会可以做到
- 为什么要对堆栈中的内存使用自定义动态内存分配?
- 我如何转换一个基于动态的,基于指针的int堆栈,以便将其打印成字符串
- 当一个类在构造函数处动态分配自己时,为什么会发生堆栈溢出而不是 std::bad_alloc
- 堆栈动态和堆栈动态数组
- 附加到具有非动态分配堆栈的向量
- 可以作为固定大小(堆栈)和动态大小(堆)工作的数组封装程序
- 链表与动态数组用于使用向量类实现堆栈
- 除了堆栈和堆模型之外,还为C++实现了哪些其他形式的自动和动态存储
- 带有指针的动态堆栈结构
- 动态堆栈、编译时出错、dev编译器和g++
- C++ 中的动态堆栈数据类型声明
- 如何显示动态堆栈和队列(c++)中的所有元素
- C++Visual Studio 2010,在实现动态堆栈时编译错误C3867
- 动态堆栈分配(来自c++思维)