【问题标题】:C# How do I compare two sorted sets?C#如何比较两个排序集?
【发布时间】:2016-06-01 16:10:10
【问题描述】:

所以在我的程序中,我有一个包含 1000 个票证对象的列表。每个对象都有一个 int ID 和一个由 6 个数字组成的 int SortedSet。我希望用户能够输入六个数字,并将这 6 个数字与列表中 1000 个对象中每个对象的排序集中的 6 个六个数字进行比较。如果数字匹配,我希望输出对象的 ID。实现这一目标的最佳方法是什么?我是否也应该将用户输入的 6 个数字也放入 SortedSet 中?这就是我想做的事情。如果是这样,我将如何将 SortedSet 与列表中的 1000 个 SortedSet 中的每一个进行比较?我已经为此工作了两天,我的头都炸了哈哈!

希望这是有道理的!

【问题讨论】:

  • 你能展示你的尝试代码吗?
  • 你有没有尝试过?您可以考虑为接受排序集、列表或任何其他存储数字的对象编写一个比较函数。

标签: c# list collections set


【解决方案1】:

是的,把用户编号也放入一个SortedSet,然后你可以使用下面的方法来查看你的票列表中的一个集合是否等于用户条目的集合。

SortedSet<int>.CreateSetComparer().Equals(userSet, objectSet);

要获取 ID 列表,您可以执行以下操作。

IEnumerable<int> GetMatchingSetIDs(SortedSet<int> userSet)
    {
        IEqualityComparer<SortedSet<int>> setComparer = SortedSet<int>.CreateSetComparer();
        foreach (Ticket ticket in tickets) //Where ticket is your ticket class with the sortedsets and tickets is a List of tickets.
        {
            if (setComparer.Equals(ticket.Set, userSet))
            {
                yield return ticket.ID;
            }
        }
    }

【讨论】:

    【解决方案2】:

    如果我理解正确,那么你需要的是一个交叉点。

    int[] array1 = { 1, 2, 3 };
    int[] array2 = { 2, 3, 4 };
    // Call Intersect extension method.
    var intersect = array1.Intersect(array2);
    // Write intersection to screen.
    foreach (int value in intersect)
    {
        Console.WriteLine(value);
    }
    

    【讨论】:

      【解决方案3】:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-07-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-02
        • 1970-01-01
        相关资源
        最近更新 更多