C# 2.0 中堆栈集合缺少 shift 和 unshift
奇怪的是,堆栈集合似乎缺少相当基本的移位和非移位方法*,而且我正在使用 2.0,所以我不能只是扩展它们。
是否有任何合理的技术或替代集合类来使这些方法可用? 我也需要推送和弹出。
编辑:看起来我想要的集合确实是一个 deque ,幸运的是它不是原生的C# :(
目前无法使用第三方库,所以我将使用笨重的 LinkedList (我说笨重是因为读取和删除是两个操作,而移位是一个),但我想我会推荐 PowerCollections 方法或者更好的是,升级到扩展方法。
*抱歉
,我没有意识到这些是不常见的术语,我想我只是不知道在哪里可以找到它们。 API 供参考:
shift = 删除第一个元素
unshift = 在集合开头插入元素
Bizarrely the stack collection seems to be missing the rather basic shift and unshift methods* and I'm working in 2.0 so I can't just extend them.
Is there any reasonable technique or alternative collection class to get these methods available? I need push and pop as well.
Edit: looks like the collection I want is indeed a deque which is happily not native to C# :(
Can't use third party libraries at this time so I'll be going with the clunky LinkedList (I say clunky because reading and removing are two operations where shift would be one) but I think I'd recommend the PowerCollections approach to anyone who could use it. Or better yet, upgrading to extension methods.
sigh
* Apologies, I didn't realise these were uncommon terms, I thought I just didn't know where to find them in the API. For reference:
shift = remove first element
unshift = insert element at beginning of collection
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我会说使用
LinkedList
。 它有从前面添加和删除的方法,以及从后面添加和删除的方法。 我从来没有听说过转移和不变,但我想这就是它的意思。I would say use a
LinkedList<T>
. It has methods for adding and removing from the front, as well as adding and removing from the back. I've never heard of shifting and unshifting, but I'm assuming that's what it means.从未听说过堆栈中的移位/取消移位。 不过,Stack 类确实提供了
Pop
、Peek
和Push
。Never heard of shift/unshift in a stack. The Stack class does provide
Pop
,Peek
, andPush
though.如果您想要移位/取消移位方法,那么您使用了错误的类。
堆栈
是一种后进先出 (LIFO) 数据结构。如果您希望在不弹出和推送的情况下进行移位/取消移位,请使用
队列
。 如果您想要两者,我建议使用 PowerCollections 中的Deque
图书馆You are using the wrong class if you want a shift/unshift method. A
stack
is a Last-In First-Out (LIFO) data structure.If you want shift/unshift without pop and push, use a
Queue
. If you want both, I recommend usingDeque
from the PowerCollections library只要您使用面向 2.0 的 C# 3.0,就可以伪造扩展方法。
您能描述一下移位/取消移位操作是什么吗?
You can fake extension methods as long as you are using C# 3.0 targeting 2.0.
Can you describe what the shift/unshift operations are?
根据定义,Stack 类表示一种使用后进先出 (LIFO) 技术添加和删除元素来管理集合中元素的方法。 LIFO 只是意味着添加到集合中的最后一个元素将自动成为第一个被删除的元素。
您想要的功能是自定义的,但可以通过以下方式轻松实现
,似乎这就是全部:)
By definition Stack class represents a way of managing elements in a collection using the Last In First Out (LIFO) technique for adding and removing elements. LIFO simply means that the last element added to a collection will automatically be the first one removed.
The functionality you want from it is something custom, but easily can be achieved in following way
and seems this it-s all :)
这并不完全是最好的,但它接近于具有shift/unshift和push/pop的Javascript数组。 它不会隐藏内部工作原理,您可以索引任何您想要的项目。 不过我有基本的功能。
This is not exactly the best, but it comes close to being a Javascript array with shift/unshift and push/pop. Its does not hide the inner workings, and you can index any item you want. I has the basic functionality though.
Unshift
不会返回 Stack 中的元素数量,您可以使用Stack.Count
属性来返回。另外,还有 Stack.Peek,可以获取第一个元素而不删除它。
堆栈; 类
Unshift
doesn't return the number of elements in the Stack, you have theStack.Count
property for that.Also, there's
Stack.Peek
, to get the first element without removing it.Stack<T> class