Home > Backend Development > C++ > How Can I Retrieve Multiple Keys Associated with a Single Value in a Generic Dictionary?

How Can I Retrieve Multiple Keys Associated with a Single Value in a Generic Dictionary?

Mary-Kate Olsen
Release: 2025-01-21 03:42:10
Original
710 people have browsed it

How Can I Retrieve Multiple Keys Associated with a Single Value in a Generic Dictionary?

Retrieve multiple keys for a given value from a generic dictionary

Retrieving a value based on a given key from a .NET generic dictionary is very simple using indexer syntax. However, since there may be multiple keys with the same value, retrieving the key corresponding to a specified value may be more challenging.

Introduction to BiDictionary data structure

To solve this problem, the BiDictionary data structure was developed, allowing bidirectional mapping between keys and values. It maintains two internal dictionaries:

  • firstToSecond: A list that maps first-type keys to second-type values.
  • secondToFirst: A list that maps second type values ​​to first type keys.

Implementation details

The BiDictionary class provides methods to add key-value pairs and retrieve values ​​based on either key type. For example:

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

IList<int> betaKeys = greek["Beta"]; // 返回 [2, 5]</code>
Copy after login

This implementation uses an empty list as the default return value for non-existent keys, ensuring that you always receive a list, even if it is empty.

Customizable indexer

For convenience, BiDictionary includes customizable indexers that provide direct access to the internal dictionary based on the calling key type. This simplifies access to values ​​by allowing you to use indexer syntax:

<code class="language-csharp">BiDictionary<int, string> greek = new BiDictionary<int, string>();
greek.Add(1, "Alpha");
greek.Add(2, "Beta");

string secondGreek = greek[2]; // 返回 "Beta"</code>
Copy after login

Example usage

The provided code demonstrates the functionality of BiDictionary:

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

ShowEntries(greek, "Alpha"); // 打印 "Alpha: [1]"
ShowEntries(greek, "Beta"); // 打印 "Beta: [2, 5]"
ShowEntries(greek, "Gamma"); // 打印 "Gamma: []"</code>
Copy after login

This example demonstrates how to retrieve the key corresponding to a given value, and it handles the case of non-existent values ​​gracefully by returning an empty list.

The above is the detailed content of How Can I Retrieve Multiple Keys Associated with a Single Value in a Generic Dictionary?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template