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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 17:19:54  来源:igfitidea点击:

c# equivalent for c++ vector or deque

c#c++vectordeque

提问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

Consider System.Collections.Generic.Listand other from System.Collection.Genericthey 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++等价物相同。
此外,可能还有更多容器供您使用。看这里

回答by Dale Wick

For a C# vector, a good candidate is System.Collection.Generic.Listas others mentioned.
The closest to the deque in C++ would be System.Collection.Generic.LinkedListwhich is a doubly linked list.

对于 C# vectorSystem.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.

尽管您不能进行索引操作,但链表可能是最接近于在恒定时间内实现先出队和最后出队的方法之一。

https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.linkedlist-1.removefirst?view=netframework-4.8

https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.linkedlist-1.removefirst?view=netframework-4.8