c# 等效于 c++ 向量或双端队列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15599127/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
c# equivalent for c++ vector or deque
提问by Ivaylo Strandjev
I am almost certain this should be a duplicate but I searched for some time and could not find the answer. What should I use in C# to replace C++ vector and deque efficiently. That is I need a structure that supports direct indexing effieciently and also supports delete from one or both ends(depending on vector or deque case) again in an efficient manner.
我几乎可以肯定这应该是重复的,但我搜索了一段时间并找不到答案。我应该在C#中使用什么来代替C ++向量和deque有效。那就是我需要一种结构,它既能有效地支持直接索引,又能以有效的方式再次支持从一端或两端删除(取决于向量或双端队列的情况)。
In java I usually use ArrayList at least for vector but for C# I found this sourcethat states:
ArrayList resizes dynamically. As elements are added, it grows in capacity to accommodate them. It is most often used in older C# programs.
. So what is the new way to do this? And again what do I do for the deque case?
在java中我通常使用ArrayList至少向量但对于C#我发现这个来源各国:
ArrayList resizes dynamically. As elements are added, it grows in capacity to accommodate them. It is most often used in older C# programs.
。那么有什么新方法可以做到这一点呢?再说一遍,我该怎么办 deque 案例?
采纳答案by Matthew Watson
There's no built-in Deque container, but there are several implementations available.
没有内置的 Deque 容器,但有几种可用的实现。
Here's a good one from Stephen Cleary. This provides O(1) operations to index and also to insert at the beginning and append at the end.
这是Stephen Cleary的一篇好文章。这提供了 O(1) 操作来索引以及在开头插入并在结尾追加。
The C# equivalent to Vector is List<T>
. Indexed access is O(1), but insertion or removal is O(N) (other than Inserting at the end, which is O(1)).
与 Vector 等效的 C# 是List<T>
. 索引访问是 O(1),但插入或删除是 O(N)(除了在末尾插入,这是 O(1))。
回答by bash.d
回答by Dale Wick
For a C# vector
, a good candidate is System.Collection.Generic.List
as others mentioned.
The closest to the deque in C++ would be System.Collection.Generic.LinkedList
which is a doubly linked list.
对于 C# vector
,System.Collection.Generic.List
正如其他人提到的那样,一个很好的候选者。
最接近 C++ 中的双端队列System.Collection.Generic.LinkedList
是双向链表。
回答by re3el
Though you cannot do an index operation, linkedlist is probably one of the closest in implementing dequeue first and dequeue last in constant time.
尽管您不能进行索引操作,但链表可能是最接近于在恒定时间内实现先出队和最后出队的方法之一。