简体   繁体   English

c# 相当于 c++ 向量或双端队列

[英]c# equivalent for c++ vector or 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 .我应该在 C# 中使用什么来有效地替换 C++ 向量和双端队列。 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.在 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. 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?我又该为 deque 案例做什么?

There's no built-in Deque container, but there are several implementations available.没有内置的 Deque 容器,但有几种可用的实现。

Here's a good one from Stephen Cleary .这是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> .与 Vector 等效的 C# 是List<T> Indexed access is O(1), but insertion or removal is O(N) (other than Inserting at the end, which is O(1)).索引访问是 O(1),但插入或删除是 O(N)(除了在末尾插入,这是 O(1))。

For a C# vector , a good candidate is System.Collection.Generic.List as others mentioned.对于 C# vector ,一个很好的候选者是System.Collection.Generic.List正如其他人提到的那样。
The closest to the deque in C++ would be System.Collection.Generic.LinkedList which is a doubly linked list.最接近 C++ 中双端队列的是System.Collection.Generic.LinkedList ,它是一个双向链表。

Consider System.Collections.Generic.List and other from System.Collection.Generic they serve the same purpose as their C++ equivalents.考虑System.Collections.Generic.List和来自其它System.Collection.Generic它们用于相同的目的的C++当量。
Additionally, there might be more containers for you.此外,可能还有更多容器供您使用。 Look here .这里

Deque is not in C#, but we can archive functionality by Vector and List, Below is a sample program to archive Deque by List. Deque 不在 C# 中,但我们可以通过 Vector 和 List 归档功能,下面是一个通过 List 归档 Deque 的示例程序。

using System;
using System.Collections.Generic;

public class GFG{
    
// Function to generate the array by
// inserting array elements one by one
// followed by reversing the array
static void generateArray(int []arr, int n)
{
    
    // Doubly ended Queue
    List<int> ans = new List<int>();
 
    // Start adding functionality at both end
    // Iterate over the array 
    // Even no at the front and odd no at the rear
    for(int i = 0; i < n; i++) 
    {
        
        // Push array elements
        // alternately to the front
        // and back
        if (arr[i]%2==0)
            ans.Insert(0,arr[i]);
        else
            ans.Add(arr[i]);
    }

    printDeque(ans);
    // Output 8 6 4 2 6 5 1 3
    
    // Start removing functionality at both end
    // Let i want to remove first(8) and last(3) element from Deque

    ans.RemoveAt(0);
    printDeque(ans);
    // Output 6 4 2 6 5 1 3 
    ans.RemoveAt(ans.Count-1);
    printDeque(ans);
    // Output 6 4 2 6 5 1
    
    
    
}
static void printDeque(List<int> q){
    // Print the elements
    // of the Deque
    foreach(int x in q)
    {
        Console.Write(x + " ");
    }
    Console.WriteLine();
}
    
// Driver Code
public static void Main(String[] args)
{
    
    int []arr = {5, 6, 1, 2, 3, 4,6, 8 };
    int n = arr.Length;
    generateArray(arr, n);
}
}

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

For me, these simple extension methods were enough.对我来说,这些简单的扩展方法就足够了。

using System.Linq;
using System.Collections.Generic;
-----
-----


        public static T consume<T>(this List<T> list, int index) {
            if (list.Count < index) return default(T);
            var item = list[index];
            list.RemoveAt(index);
            return item;
        }
        public static T pop<T>(this List<T> list) {
            return list.consume(list.Count - 1);
        }
        public static T dequeue<T>(this List<T> list) {
            return list.consume(0);
        }
        public static void push<T>(this List<T> list, T item) {
            list.Add(item);
        }
        public static void enqueue<T>(this List<T> list, T item) {
            list.Add(item);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM