【问题标题】:Dictionaries with operator overloading具有运算符重载的字典
【发布时间】:2017-05-29 22:53:34
【问题描述】:

我想知道是否可以使用运算符重载将 2 个字典合为一个,但我远离正确的模式,这是我的代码。

    class Program
    {
     static void Main(string[] args)
     {
         int[] array1key = new int[] { 0, 1, 3, 7 };
         int[] array1values = new int[] { 5, 7, -3, 9 };

         int[] array2key = new int[] { 0, 2 };
         int[] array2values = new int[] { 2, 4 };

         Dictionary<int, int> Polinomio1 = new Dictionary<int, int>();
         Dictionary<int, int> Polinomio2 = new Dictionary<int, int>();

         for (int i = 0; i < array1key.Length; i++)
         {
             Polinomio1.Add(array1key[i], array1values[i]);
         }
         for (int i = 0; i < array2key.Length; i++)
         {
             Polinomio2.Add(array2key[i], array2values[i]);
         }
         Dictionary<int, int> Sumar = Polinomio1 + Polinomio2;
     }
   }
   class Polinomio
   {
   public Dictionary<int,int> Polinomio1 = new Dictionary<int,int>();
   public static Dictionary<int,int> operator + (Dictionary<int,int> 
   Polinomio1,Dictionary<int,int> Polinomio2)
   {
        return Polinomio1 + Polinomio2;
   }
   }

【问题讨论】:

  • 正如错误消息所解释的,当您尝试重载运算符时,运算符重载必须包含至少一个与传入的类具有相同类型的参数。您通常不能重载+ 或您没有编写的课程上的任何其他运算符。当然,您可以继承该类并将重载放在那里。但当然,只有当至少有一个操作数是该派生类时,它才会起作用。坦率地说,我对字典重载+ 的价值持怀疑态度。对于操作员来说,没有单一的、明显的含义。一个命名良好的方法会更聪明。
  • 那我怎么能用方法呢?
  • 什么意思?您只需编写一个带有两个参数的方法,每个输入字典一个。做任何你认为是字典“总和”的方法,并返回结果。就像任何其他方法一样。

标签: c# dictionary operator-overloading .net-standard


【解决方案1】:

这是一个复杂的情况,但我可以给你一些想法,你可以选择最适合你的。

首先你不能重载一个操作符,如果其中一个参数在 重载参数列表与包含的类型不同 类,因此,如果您在 Polinomio 类中,则必须提供此类型的参数之一,这是您不想要的,因为您希望为 Dictionary&lt;&gt; 类重载运算符 +。 参考这个问题here包含类有什么问题。

  1. 解决这个问题的一种方法是从Dictionary&lt;&gt; 类本身继承。让我们看下面的例子来更好地理解我的意思。

    class Program
    {
        static void Main(string[] args)
        {
            //Made those in same size to not throw an exception
            int[] array1Key = new int[] { 0, 1, 3, 7 };
            int[] array1Values = new int[] { 5, 7, -3, 9 };
    
            int[] array2Key = new int[] { 0, 2, 3, 4 };
            int[] array2Values = new int[] { 2, 4, 4, 8 };
    
            //Create Polinomio object which will serve as `Dictionary<>` in this case
            Polinomio polinomio1 = new Polinomio();
            Polinomio polinomio2 = new Polinomio();
    
            for (int i = 0; i < array1Key.Length; i++)
            {
                polinomio1.Add(array1Key[i], array1Values[i]);
            }
            for (int i = 0; i < array2Key.Length; i++)
            {
                polinomio2.Add(array2Key[i], array2Values[i]);
            }
            Dictionary<int, int> sum = polinomio1 + polinomio2;
            for (int i = 0; i < sum.Count; i++)
            {
                Console.WriteLine($"{sum.Keys.ElementAt(i)} {sum.Values.ElementAt(1)}");
            }
            Console.ReadLine();
        }
    
    }
    public class Polinomio : Dictionary<int, int> //inheritance
    {
        public static Dictionary<int, int> operator +(Polinomio p1, Polinomio p2)
        {
            if (p1.Count != p2.Count)
            {
                throw new Exception("Not the same Size");
            }
            Dictionary<int, int> dictionaryTemp = new Dictionary<int, int>();
            for (int i = 0; i < p1.Count; i++)
            {
                dictionaryTemp.Add(p1.Keys.ElementAt(i) + p1.Keys.ElementAt(i), p1.Values.ElementAt(i) + p2.Values.ElementAt(1));
            }
            return dictionaryTemp;
        }
    }
    
    1. 另一种解决方案是向Dictionary&lt;&gt; 类添加扩展方法,如下所示:

      public static Dictionary<int, int> Sum(this Dictionary<int, int> p1, Dictionary<int, int> p2) 
      {
         if (p1.Count != p2.Count)
         {
             throw new Exception("Not the same Size");
         }
         Dictionary<int, int> dictionaryTemp = new Dictionary<int, int>();
         for (int i = 0; i < p1.Count; i++)
         {
             dictionaryTemp.Add(p1.Keys.ElementAt(i) + p1.Keys.ElementAt(i), p1.Values.ElementAt(i) + p2.Values.ElementAt(1));
         }
         return dictionaryTemp;
      }
      

你可以这样称呼它:

Dictionary<int, int> sum = polinomio1.Sum(polinomio2);
for (int i = 0; i < sum.Count; i++)
{
   Console.WriteLine($"{sum.Keys.ElementAt(i)} {sum.Values.ElementAt(1)}");
}

【讨论】:

  • 很高兴它有帮助??
【解决方案2】:

我可以推荐这个吗:

    public static Dictionary<int, double> Addition(this Dictionary<int, double> p1, Dictionary<int, double> p2)
{
    if (p1.Count != p2.Count)
    {
        throw new Exception("Not the same Size");
    }
    Dictionary<int, double> dictionaryTemp = new Dictionary<int, double>();
    foreach(var key in p1.Keys)
    {
        dictionaryTemp.Add(key, p1[key] + p2[key]);
    }
    return dictionaryTemp;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多