第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

獲取通用字典指定值的多個鍵?

獲取通用字典指定值的多個鍵?

炎炎設計 2019-07-08 17:31:45
獲取通用字典指定值的多個鍵?很容易從.NET通用字典中獲得鍵的值:Dictionary<int, string> greek = new Dictionary<int, string>();greek.Add(1, "Alpha");greek.Add(2, "Beta");string secondGreek = greek[2];  // Beta但是,嘗試獲取給定值的鍵并不是那么簡單,因為可能有多個鍵:int[] betaKeys = greek.WhatDoIPutHere("Beta");  // expecting single 2
查看完整描述

3 回答

?
慕仙森

TA貢獻1827條經驗 獲得超8個贊

好的,這是多個雙向版本:

using System;using System.Collections.Generic;using System.Text;class BiDictionary<TFirst, TSecond>{
    IDictionary<TFirst, IList<TSecond>> firstToSecond = new Dictionary<TFirst, IList<TSecond>>();
    IDictionary<TSecond, IList<TFirst>> secondToFirst = new Dictionary<TSecond, IList<TFirst>>();

    private static IList<TFirst> EmptyFirstList = new TFirst[0];
    private static IList<TSecond> EmptySecondList = new TSecond[0];

    public void Add(TFirst first, TSecond second)
    {
        IList<TFirst> firsts;
        IList<TSecond> seconds;
        if (!firstToSecond.TryGetValue(first, out seconds))
        {
            seconds = new List<TSecond>();
            firstToSecond[first] = seconds;
        }
        if (!secondToFirst.TryGetValue(second, out firsts))
        {
            firsts = new List<TFirst>();
            secondToFirst[second] = firsts;
        }
        seconds.Add(second);
        firsts.Add(first);
    }

    // Note potential ambiguity using indexers (e.g. mapping from int to int)
    // Hence the methods as well...
    public IList<TSecond> this[TFirst first]
    {
        get { return GetByFirst(first); }
    }

    public IList<TFirst> this[TSecond second]
    {
        get { return GetBySecond(second); }
    }

    public IList<TSecond> GetByFirst(TFirst first)
    {
        IList<TSecond> list;
        if (!firstToSecond.TryGetValue(first, out list))
        {
            return EmptySecondList;
        }
        return new List<TSecond>(list); // Create a copy for sanity
    }

    public IList<TFirst> GetBySecond(TSecond second)
    {
        IList<TFirst> list;
        if (!secondToFirst.TryGetValue(second, out list))
        {
            return EmptyFirstList;
        }
        return new List<TFirst>(list); // Create a copy for sanity
    }}class Test{
    static void Main()
    {
        BiDictionary<int, string> greek = new BiDictionary<int, string>();
        greek.Add(1, "Alpha");
        greek.Add(2, "Beta");
        greek.Add(5, "Beta");
        ShowEntries(greek, "Alpha");
        ShowEntries(greek, "Beta");
        ShowEntries(greek, "Gamma");
    }

    static void ShowEntries(BiDictionary<int, string> dict, string key)
    {
        IList<int> values = dict[key];
        StringBuilder builder = new StringBuilder();
        foreach (int value in values)
        {
            if (builder.Length != 0)
            {
                builder.Append(", ");
            }
            builder.Append(value);
        }
        Console.WriteLine("{0}: [{1}]", key, builder);
    }}


查看完整回答
反對 回復 2019-07-08
?
白衣非少年

TA貢獻1155條經驗 獲得超0個贊

正如其他人所說,字典中沒有從值到鍵的映射。

我剛剛注意到您想要從Value映射到多個鍵-我把這個解決方案留給單個值版本,但是接下來我將為多條目雙向映射添加另一個答案。

這里通常采用的方法是有兩本字典-一種是映射方式,另一種是映射方式。將它們封裝在一個單獨的類中,并計算出當您擁有重復的鍵或值時要做什么(例如拋出異常、覆蓋現(xiàn)有條目或忽略新條目)。就我個人而言,我可能會選擇拋出一個例外-它使成功行為更容易定義。就像這樣:

using System;using System.Collections.Generic;class BiDictionary<TFirst, TSecond>{
    IDictionary<TFirst, TSecond> firstToSecond = new Dictionary<TFirst, TSecond>();
    IDictionary<TSecond, TFirst> secondToFirst = new Dictionary<TSecond, TFirst>();

    public void Add(TFirst first, TSecond second)
    {
        if (firstToSecond.ContainsKey(first) ||
            secondToFirst.ContainsKey(second))
        {
            throw new ArgumentException("Duplicate first or second");
        }
        firstToSecond.Add(first, second);
        secondToFirst.Add(second, first);
    }

    public bool TryGetByFirst(TFirst first, out TSecond second)
    {
        return firstToSecond.TryGetValue(first, out second);
    }

    public bool TryGetBySecond(TSecond second, out TFirst first)
    {
        return secondToFirst.TryGetValue(second, out first);
    }}class Test{
    static void Main()
    {
        BiDictionary<int, string> greek = new BiDictionary<int, string>();
        greek.Add(1, "Alpha");
        greek.Add(2, "Beta");
        int x;
        greek.TryGetBySecond("Beta", out x);
        Console.WriteLine(x);
    }}


查看完整回答
反對 回復 2019-07-08
?
MM們

TA貢獻1886條經驗 獲得超2個贊

字典并不是真正像這樣工作的,因為雖然鍵的唯一性得到了保證,但是值的唯一性卻不是這樣的。

var greek = new Dictionary<int, string> { { 1, "Alpha" }, { 2, "Alpha" } };

你希望得到什么greek.WhatDoIPutHere("Alpha")?

因此,您不能期望將這樣的內容滾到框架中。您需要自己的方法來實現(xiàn)自己的獨特用途-您想返回一個數(shù)組(或IEnumerable<T>)?如果存在具有給定值的多個鍵,是否要拋出異常?如果沒有呢?

就我個人而言,我會選擇一個可列舉的,如下所示:

IEnumerable<TKey> KeysFromValue<TKey, TValue>(this Dictionary<TKey, TValue> dict, TValue val){
    if (dict == null)
    {
        throw new ArgumentNullException("dict");
    }
    return dict.Keys.Where(k => dict[k] == val);}var keys = greek.KeysFromValue("Beta");
    int exceptionIfNotExactlyOne = greek.KeysFromValue("Beta").Single();


查看完整回答
反對 回復 2019-07-08
  • 3 回答
  • 0 關注
  • 666 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號