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 ++ vector和deque。也就是说,我需要一种能够高效地支持直接索引的结构,并且还支持以有效的方式从一端或两端(取决于向量或双端情况)删除。
In java I usually use ArrayList at least for vector but for C# I found this source that 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动态调整大小。随着元素的增加,它会增加容纳它们的能力。它最常用于较旧的C#程序。那么新的方法是什么?再次,我该如何处理deque案件?
3 个解决方案
#1
18
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.
斯蒂芬克利里这是一个很好的。这提供了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
#2
9
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#向量,一个好的候选者是System.Collection.Generic.List,正如其他人提到的那样。 C ++中最接近deque的是System.Collection.Generic.LinkedList,它是一个双向链表。
#3
3
Consider System.Collections.Generic.List
and other from System.Collection.Generic
they serve the same purpose as their C++
equivalents.
Additionally, there might be more containers for you. Look here.
考虑System.Collections.Generic.List和System.Collection.Generic中的其他函数,它们与C ++等价物具有相同的用途。此外,您可能还有更多容器。看这里。
#1
18
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.
斯蒂芬克利里这是一个很好的。这提供了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
#2
9
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#向量,一个好的候选者是System.Collection.Generic.List,正如其他人提到的那样。 C ++中最接近deque的是System.Collection.Generic.LinkedList,它是一个双向链表。
#3
3
Consider System.Collections.Generic.List
and other from System.Collection.Generic
they serve the same purpose as their C++
equivalents.
Additionally, there might be more containers for you. Look here.
考虑System.Collections.Generic.List和System.Collection.Generic中的其他函数,它们与C ++等价物具有相同的用途。此外,您可能还有更多容器。看这里。