首頁 後端開發 C++ 如何檢索與通用字典中的特定值關聯的多個鍵?

如何檢索與通用字典中的特定值關聯的多個鍵?

Jan 21, 2025 am 03:37 AM

How to Retrieve Multiple Keys Associated with a Specific Value in a Generic Dictionary?

在泛型字典中取得特定值的多個鍵

在 .NET 中,檢索與泛型字典中鍵關聯的值非常簡單。但是,確定給定值的鍵並不容易,尤其是在多個鍵可能對應於相同值時。

問題

考慮以下程式碼片段:

Dictionary<int, string> greek = new Dictionary<int, string>();
greek.Add(1, "Alpha");
greek.Add(2, "Beta");

int[] betaKeys = greek.WhatDoIPutHere("Beta"); // 预期结果为单个 2
登入後複製

目標是取得一個包含映射到值「Beta」的鍵的陣列。

我們可以建立一個自訂雙向字典,它能夠擷取鍵和值:

using System;
using System.Collections.Generic;
using System.Linq;

class BiDictionary<TFirst, TSecond>
{
    private IDictionary<TFirst, IList<TSecond>> firstToSecond = new Dictionary<TFirst, IList<TSecond>>();
    private IDictionary<TSecond, IList<TFirst>> secondToFirst = new Dictionary<TSecond, IList<TFirst>>();

    public void Add(TFirst first, TSecond second)
    {
        IList<TSecond> seconds;
        IList<TFirst> firsts;

        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);
    }

    public IEnumerable<TSecond> GetByFirst(TFirst first)
    {
        IList<TSecond> list;
        return firstToSecond.TryGetValue(first, out list) ? list : Enumerable.Empty<TSecond>();
    }

    public IEnumerable<TFirst> GetBySecond(TSecond second)
    {
        IList<TFirst> list;
        return secondToFirst.TryGetValue(second, out list) ? list : Enumerable.Empty<TFirst>();
    }
}
登入後複製

要使用此雙向字典,我們可以將前面範例中的程式碼替換為:

BiDictionary<int, string> greek = new BiDictionary<int, string>();
greek.Add(1, "Alpha");
greek.Add(2, "Beta");
greek.Add(5, "Beta");

IEnumerable<int> betaKeys = greek.GetBySecond("Beta");

foreach (int key in betaKeys)
{
    Console.WriteLine(key); // 2, 5
}
登入後複製

此解決方案有效地提供了一種方法來檢索與多值字典中指定值關聯的所有鍵。

以上是如何檢索與通用字典中的特定值關聯的多個鍵?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱門文章

倉庫:如何復興隊友
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱門文章

倉庫:如何復興隊友
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱門文章標籤

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

Gulc:從頭開始建造的C庫 Gulc:從頭開始建造的C庫 Mar 03, 2025 pm 05:46 PM

Gulc:從頭開始建造的C庫

c語言函數返回值的類型有哪些?返回值是由什麼決定的? c語言函數返回值的類型有哪些?返回值是由什麼決定的? Mar 03, 2025 pm 05:52 PM

c語言函數返回值的類型有哪些?返回值是由什麼決定的?

c語言函數格式字母大小寫轉換步驟 c語言函數格式字母大小寫轉換步驟 Mar 03, 2025 pm 05:53 PM

c語言函數格式字母大小寫轉換步驟

c語言函數的定義和調用規則是什麼 c語言函數的定義和調用規則是什麼 Mar 03, 2025 pm 05:53 PM

c語言函數的定義和調用規則是什麼

distinct用法和短語分享 distinct用法和短語分享 Mar 03, 2025 pm 05:51 PM

distinct用法和短語分享

c語言函數返回值在內存保存在哪裡? c語言函數返回值在內存保存在哪裡? Mar 03, 2025 pm 05:51 PM

c語言函數返回值在內存保存在哪裡?

C標準模板庫(STL)如何工作? C標準模板庫(STL)如何工作? Mar 12, 2025 pm 04:50 PM

C標準模板庫(STL)如何工作?

如何有效地使用STL(排序,查找,轉換等)的算法? 如何有效地使用STL(排序,查找,轉換等)的算法? Mar 12, 2025 pm 04:52 PM

如何有效地使用STL(排序,查找,轉換等)的算法?

See all articles