【问题标题】:c# equivalent for c++ vector or dequec# 等效于 c++ 向量或双端队列
【发布时间】:2013-03-14 00:12:36
【问题描述】:

我几乎可以肯定这应该是重复的,但我搜索了一段时间并找不到答案。我应该在 C# 中使用什么来替换 C++ 向量和双端队列有效。那就是我需要一种结构,它既能有效地支持直接索引,又能以一种有效的方式再次支持从一端或两端删除(取决于向量或双端队列)。

在 java 中,我通常至少将 ArrayList 用于向量,但对于 C#,我发现 this source 指出: ArrayList resizes dynamically. As elements are added, it grows in capacity to accommodate them. It is most often used in older C# programs.。那么这样做的新方法是什么?我又该如何处理双端队列的情况?

【问题讨论】:

标签: c# c++ vector deque


【解决方案1】:

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);
}
}

【讨论】:

  • 这是一个非常糟糕的解决方案,因为插入到列表中的速度比双端队列需要的慢得多。您可以考虑一个有两个列表的实现。要遍历整个双端队列实现,您将首先在一个列表上向后迭代,然后在另一个列表上向前迭代。这使您可以通过简单地从列表中添加和删除来快速添加和删除双端队列的两端。如已接受的答案中所述,存在此库。
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    没有内置的 Deque 容器,但有几种可用的实现。

    这里是a good one from Stephen Cleary。这提供了 O(1) 操作来索引以及在开头插入并在结尾追加。

    与 Vector 等效的 C# 是 List&lt;T&gt;。索引访问是 O(1),但插入或删除是 O(N)(除了在末尾插入,即 O(1))。

    【讨论】:

    • List 中的索引操作效率如何?我以为它会简单地遍历列表
    • @IvayloStrandjev 这不是一个双向链表,它是一个向量。
    • @Ivaylo:不,它不是一个链表,它是一个数组的包装器,所以索引只是使用索引直接访问底层数组,所以它是一个 O(1) 操作。 (几乎和 C++ 向量一样)
    • 在中间插入时O(N),最后插入O(1)(一般)
    • 写一个基本的Deque真的有必要引用其他15个包吗?
    【解决方案4】:

    对于 C# vector,一个好的候选者是 System.Collection.Generic.List,正如其他人提到的那样。
    C++ 中最接近双端队列的是System.Collection.Generic.LinkedList,它是一个双向链表。

    【讨论】:

    • +1,如果您不需要访问中间的元素,链接列表似乎是最好的双端队列解决方案。
    • 推荐LinkedList 作为deque 替换是不正确且不准确的。它们是完全不同的野兽(前者没有连续分配)并且绝对不可替代。
    • @MahmoudAl-Qudsi 最终用户看到LinkedList 和双端队列之间有什么区别?
    【解决方案5】:

    考虑System.Collections.Generic.List 和System.Collection.Generic 中的其他人,它们的用途与C++ 等效项相同。
    此外,可能还有更多容器适合您。看here。

    【讨论】:

    • 是 Deque 还是 Dequeue?我似乎找不到关于 Deque 的任何信息。顺便说一句,我不会投反对票。
    猜你喜欢
    • 2010-12-28
    • 1970-01-01
    • 2015-05-01
    • 2017-04-22
    • 2017-07-08
    • 2012-07-25
    • 1970-01-01
    • 1970-01-01
    • 2017-02-28
    相关资源
    最近更新 更多