【问题标题】:What is the equivalent of a map of maps in C#什么相当于 C# 中的地图地图
【发布时间】:2016-04-10 05:40:50
【问题描述】:

在 C++ 中,我使用的是 map<string, map<string, string>> Any_name;。这在 C# 中的等价物是什么?

【问题讨论】:

  • 这不是多图。它是具有映射值类型的映射。多映射是可以包含相同键的少量值或相同键的映射。

标签: c# c++


【解决方案1】:

应该是Dictionary<string, Dictionary<string, string>>,MSDN 上的文档。

根据评论更新了示例:

Dictionary<string, Dictionary<string, string>> map = new Dictionary<string, Dictionary<string, string>>();
map["test1"] = new Dictionary<string, string>();
map["test1"]["test2"] = "anything";

【讨论】:

  • 我使用了它,但是当我想保存类似 data["test1"]["test2"] = "anything";
  • 你应该先初始化字典,检查答案中更新的代码示例。
【解决方案2】:

由于 std::map 已排序,因此 C#(通常为 .NET)等效为 SortedDictionary:

using System.Collections.Generic;
...
SortedDictionary<string, SortedDictionary<string, string>> Any_name = new SortedDictionary<string, SortedDictionary<string, string>>();

【讨论】:

    【解决方案3】:
    public interface IMultiMap<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>>, IEnumerable
    {
        IEnumerable<TValue> this[TKey key] { get; set; }
        int Count { get; }
        IEnumerable<TKey> Keys { get; }
        IEnumerable<TValue> Values { get; }
    
        void Add(TKey key, TValue value);
        void Clear();
        bool Contains(TKey Key, TValue value);
        bool Contains(KeyValuePair<TKey, TValue> item);
        bool ContainsKey(TKey key);
        bool Remove(KeyValuePair<TKey, TValue> item);
        bool Remove(TKey key);
        bool Remove(TKey key, TValue value);
        bool TryGetValue(TKey key, out IEnumerable<TValue> value);
    }
    
    public class MultiMap<TKey, TValue> : IMultiMap<TKey, TValue>
    {
        private SortedDictionary<TKey, SortedSet<TValue>> _Items;
    
        public MultiMap()
        {
            _Items = new SortedDictionary<TKey, SortedSet<TValue>>();
        }
    
        public IEnumerable<TValue> this[TKey key]
        {
            get
            {
                return _Items[key];
            }
    
            set
            {
                _Items[key] = new SortedSet<TValue>(value);
            }
        }
    
        public int Count
        {
            get
            {
                return _Items.SelectMany(kvp => kvp.Value).Count();
            }
        }
    
        public IEnumerable<TKey> Keys
        {
            get
            {
                return _Items.Keys;
            }
        }
    
        public IEnumerable<TValue> Values
        {
            get
            {
                return _Items.SelectMany(kvp => kvp.Value);
            }
        }
    
        public void Add(TKey key, TValue value)
        {
            if (!_Items.ContainsKey(key))
                _Items.Add(key, new SortedSet<TValue>());
    
            _Items[key].Add(value);
        }
    
        public void Add(KeyValuePair<TKey, TValue> item)
        {
            Add(item.Key, item.Value);
        }
    
        public void Clear()
        {
            _Items.Clear();
        }
    
        public bool Contains(KeyValuePair<TKey, TValue> item)
        {
            return Contains(item.Key, item.Value);
        }
    
        public bool Contains(TKey key, TValue value)
        {
            return _Items.ContainsKey(key) && _Items[key].Contains(value);
        }
    
        public bool ContainsKey(TKey key)
        {
            return _Items.ContainsKey(key);
        }
    
        public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
        {            
            return _Items.SelectMany(kvp => kvp.Value.Select(v => new KeyValuePair<TKey, TValue>(kvp.Key, v))).GetEnumerator();            
        }
    
        public bool Remove(TKey key)
        {
            return _Items.Remove(key);
        }
    
        public bool Remove(KeyValuePair<TKey, TValue> item)
        {
            return Remove(item.Key, item.Value);
        }
    
        public bool Remove(TKey key, TValue value)
        {
            if (!_Items.ContainsKey(key))
                return false;
    
            return _Items[key].Remove(value);
        }
    
        public bool TryGetValue(TKey key, out IEnumerable<TValue> value)
        {
            bool isPresent = _Items.ContainsKey(key);
            value = isPresent ? _Items[key] : null;
            return isPresent;
        }
    
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }
    

    【讨论】:

    • 虽然此代码可能会回答问题,但提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-30
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    相关资源
    最近更新 更多