在C#中分配堆栈大小?

发布于 2024-11-25 16:52:15 字数 112 浏览 3 评论 0原文

我正在为我一直在研究的字节代码语言开发虚拟机。我正在使用 System.Collections.Generic.Stack 类作为堆栈,但是有什么方法可以分配堆栈大小吗?或者我只需要编写自己的堆栈对象即可使用?

I am developing a virtual machine for a byte code language I have been working on. I am using the System.Collections.Generic.Stack class for the stack but is there any way to allocate the stack size? Or do I just have to write my own stack object to use?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

若有似无的小暗淡 2024-12-02 16:52:15

来自 Stack(Int32) 构造函数的 MSDN

栈的容量是栈中元素的数量
可以持有。当元素添加到堆栈时,容量为
通过重新分配内部空间,根据需要自动增加
数组。

如果可以估计集合的大小,请指定初始值
容量消除了执行多次调整大小的需要
向 Stack 添加元素时进行操作。

可以通过调用TrimExcess来减少容量。

此构造函数是一个 O(n) 操作,其中 n 是容量。

From MSDN for the Stack(Int32) constructor:

The capacity of a Stack is the number of elements that the Stack
can hold. As elements are added to a Stack, the capacity is
automatically increased as required by reallocating the internal
array.

If the size of the collection can be estimated, specifying the initial
capacity eliminates the need to perform a number of resizing
operations while adding elements to the Stack.

The capacity can be decreased by calling TrimExcess.

This constructor is an O(n) operation, where n is capacity.

蓬勃野心 2024-12-02 16:52:15

您可以使用初始容量构建它,但堆栈将随着需要(当您向其中添加项目时)。

如果您不希望它在超过其容量时自动添加新项目,则需要将其封装在您自己的类中或编写自己的堆栈来删除多余的项目。

You can construct it with an initial capacity, but the stack will grow as needed (as you add items to it).

If you do not want it to automatically add new items when you push it past it's capacity, you'll need to encapsulate it in your own class or write your own Stack which removes the excess items.

柏拉图鍀咏恒 2024-12-02 16:52:15

您可以使用 Stack构造函数 (Int32)用于指定堆栈的容量:

var stack = new Stack<Foo>(1024);

请注意,如果添加超过 1024 个项目,堆栈的容量将会增加。如果你不想这样,你可以在每次推送之前检查堆栈大小:

if (stack.Count == 1024)
    throw new StackOverflowException();
stack.Push(foo);

You can use the Stack<T> Constructor (Int32) to specifiy the capacity of the stack:

var stack = new Stack<Foo>(1024);

Note that the stack will grow the capacity if you add more than 1024 items. If you don't want this, you can check the stack size before each push:

if (stack.Count == 1024)
    throw new StackOverflowException();
stack.Push(foo);
梦在深巷 2024-12-02 16:52:15

您的术语还有很多不足之处,但是 Stack 有一个构造函数,允许您传递初始容量:http://msdn.microsoft.com/en-us/library/ahc986x9.aspx

堆栈可以随内存大小自由增长,因此您不用担心这个!

Your terminology leaves a lot to be desired, but Stack has a constructor that allows you to pass an initial capacity: http://msdn.microsoft.com/en-us/library/ahc986x9.aspx

The stack is free to grow as large as you have memory though, so you don't need to worry about that!

思念满溢 2024-12-02 16:52:15

堆栈(T)类

表示可变大小的后进先出 (LIFO) 集合
相同任意类型的实例。

它会动态地自行调整大小。但如果您想预分配其大小,请查看有关堆栈构造函数的文档。您可以使用重载来设置堆栈的初始大小。

Stack(T) Class

Represents a variable size last-in-first-out (LIFO) collection of
instances of the same arbitrary type.

It resizes it self dynamically. But if you want to preallocate its size then check out the documentation on the Stack's constructor. There's an overload with which you can set the stack's initial size.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文