Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# equivalent for c++ vector or deque

Tags:

c++

c#

vector

deque

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.

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?

like image 808
Ivaylo Strandjev Avatar asked Mar 24 '13 13:03

Ivaylo Strandjev


3 Answers

There's no built-in Deque container, but there are several implementations available.

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.

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)).

like image 164
Matthew Watson Avatar answered Oct 19 '22 07:10

Matthew Watson


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.

like image 32
Dale Wick Avatar answered Oct 19 '22 08:10

Dale Wick


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.

like image 2
bash.d Avatar answered Oct 19 '22 09:10

bash.d