代码之家  ›  专栏  ›  技术社区  ›  ebattulga

如何在C语言中双向使用字典#

c#
  •  6
  • ebattulga  · 技术社区  · 15 年前
    public static Dictionary<int, string> dic = new Dictionary<int, string>() { 
                {1,"anystring1"},
                {2,"anystring2"}};
    

    我需要用这个

    string str= dic[1]; // it is possible
    
    int a=dic["anystring1"]; // My dream is it
    
    5 回复  |  直到 13 年前
        1
  •  2
  •   jason    15 年前

    这不是字典的本意。你能想出一个定义并立即在你最喜欢的字典里找到匹配的单词吗? O(1) 时间?如果您想要一个具有这种类型功能的类(双向字典),您必须自己构建它(或 Google 用于Internet上的许多实现之一)。

        2
  •  7
  •   Daniel MoÅ¡mondor    15 年前

    使用另一个 Dictionary<> 并按键/值的相反顺序使用。

        3
  •  5
  •   Bob    13 年前

    这次我有点晚了,但是Linq是你在这里的朋友:

    MyDict.FirstOrDefault(pair => pair.Value == "the value you want").Key;
    

    让你做你想做的事。

        4
  •  4
  •   dherman    14 年前

    我希望它在系统库中,但是很容易自己滚动。

    下面,我将提供编写此类的框架,其用法如下:

    var twoWayDict = new TwoWayDict<string, int>();
    
    twoWayDict["zero"] = 0;
    // twoWayDict["zero"] == 0
    // twoWayDict.Reverse[0] == "zero"
    
    twoWayDict.Reverse[1] = "one";
    // twoWayDict["one"] == 1
    // twoWayDict.Reverse[1] == "one"
    

    请记住,双向字典的一个关键点是,您应该期望所有输入都是紧密耦合的。换句话说,如果您重新使用一个键或一个值,您将删除先前链接的数据:

    twoWayDict["zero"] = 0;
    
    // Then later...
    twoWayDict.Reverse[0] = "ZERO";
    // Now twoWayDict["ZERO"] == 0
    
    // Later still...
    // Exception: Key not found! "zero" was dropped when you re-used value 0
    Console.WriteLine(twoWayDict["zero"]); 
    

    最后,这里是一些示例代码。它是最小的-它应该作为任何想要充实自己版本的人的基础。注意,我实现了一个包装类,这样我就可以在不直接公开内部字典的情况下提供“reverse”属性。

    // Generics note: K indicates "key" type and V indicates "value" type
    using System.Collections.Generic;
    
    namespace YourNamespaceHere.Collections
    {
      public class TwoWayDict<K, V>
      {
        private Dictionary<K, V> _dictKV;
        private Dictionary<V, K> _dictVK;
        private ReverseDict _reverseDict;
    
        public TwoWayDict()
        {
          _dictKV = new Dictionary<K, V>();
          _dictVK = new Dictionary<V, K>();
          _reverseDict = new ReverseDict(this);
        }
    
        public ReverseDict Reverse
        {
          get { return _reverseDict; }
        }
    
        // TwoWayDict[key] -> value
        public V this[K key]
        {
          get { return _dictKV[key]; }
          set
          {
            // Remove any existing key/value pair
            Remove(key);
    
            _dictKV[key] = value;
            _dictVK[value] = key;
          }
        }
    
        public void Remove(K key)
        {
          if (_dictKV.ContainsKey(key))
          {
             _dictVK.Remove(_dictKV[key]);
             _dictKV.Remove(key);
          }
        }
    
        // Wrapper that allows TwoWayDict to expose a convenient
        // 'Reverse' property.
        public class ReverseDict
        {
          private TwoWayDict<K, V> _parent;
          public ReverseDict(TwoWayDict<K, V> parent)
          {
             _parent = parent;
          }
    
          public K this[V reverseKey]
          {
            get { return _parent._dictVK[reverseKey]; }
            set { _parent[value] = reverseKey; }
          }
    
          public void Remove(V value)
          {
            if (_parent._dictVK.ContainsKey(value))
            {
              _parent.Remove(_parent._dictVK[value]);
            }
          }
        }    
      }
    }
    
        5
  •  0
  •   Rodney P. Barbati    13 年前

    我实际上使用了一个类,它结合了一个数组列表和一个字典,这样我就可以根据添加的名称或顺序查找子节点,并在添加对象时保持它们的原始顺序。

    首先将对象添加到arraylist,然后使用所需键将arraylist中该对象的索引添加到字典中。

    这允许我以非常理想的方式通过键或位置访问,同时保持添加对象的顺序。

    要注意的gotcha区域是使用现有键添加另一个对象,这将孤立原始对象,并从向量中删除任何元素,这将导致字典中的索引损坏,指向错误的值。

    我只是想和别人分享我的两分钱——希望它能帮助别人。