.NET 中按键/值对位置随机访问 (C#)

发布于 2024-07-25 15:05:50 字数 311 浏览 4 评论 0原文

我目前正在开发一个使用 C# 的 Dictionary 容器(具体来说,SortedDictionary)的程序。 这个容器非常适合我的目的,除了一种特定情况,因为我想要随机访问。 具体来说,我使用伪随机数生成器生成随机位置,并且我需要能够访问 SortedDictionary 中的该值。 当这种情况发生时,我没有关键值。

我可能会切换到列表来解决这个问题,但会在 SortedDictionary 工作得很好的算法的其余部分中产生问题。 任何建议/解决方案将不胜感激。

我目前正在开发 Visual Studio 2005。

谢谢。

I am currently developing a program that uses C#'s Dictionary container (specifically, SortedDictionary). This container works very well for my purposes except for one specific case because I want random access. Specifically, I am generating a random position using a pseudorandom number generator and I need to be able to access that value in the SortedDictionary. At the point that this happens, I do not have a key value.

I could potentially switch to a List which would solve this problem, but would create problems in the rest of the algorithm where SortedDictionary works quite well. Any suggestions/solutions would be much appreciated.

I am currently developing Visual Studio 2005.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

红颜悴 2024-08-01 15:05:50

您可以使用 SortedList,它有一个 Values 集合,您可以通过整数索引访问该集合。

You can use a SortedList and it has a Values collection which you may access through an integer index.

久光 2024-08-01 15:05:50
    public TValue GetRandomElement<TKey, TValue>(SortedDictionary<TKey, TValue> dict)
    {
        Random randGen = new Random();
        int randIndex = randGen.Next(dict.Values.Count);
        int i = 0;
        foreach (TValue value in dict.Values)
        {
            if (i++ == randIndex)
                return value;
        }

        // this shouldn't happen unless I have a bug above or you are accessing the dictionary from multiple threads
        return default(TValue);
    }

盲目枚举 ValueCollection 并不是世界上最有效的事情。 但它完成了工作。 如果这是您的场景中的频繁操作,您应该考虑具有字典查找和随机访问所需的性能特征的混合数据结构。

    public TValue GetRandomElement<TKey, TValue>(SortedDictionary<TKey, TValue> dict)
    {
        Random randGen = new Random();
        int randIndex = randGen.Next(dict.Values.Count);
        int i = 0;
        foreach (TValue value in dict.Values)
        {
            if (i++ == randIndex)
                return value;
        }

        // this shouldn't happen unless I have a bug above or you are accessing the dictionary from multiple threads
        return default(TValue);
    }

Blindly enumerating the ValueCollection is not the most efficient thing in the world. But it gets the job done. If this is a frequent operation in your scenario, you should consider a hybrid data structure that has the performance characteristics needed for both dictionary lookup and random access.

め七分饶幸 2024-08-01 15:05:50

Linq 可以为你做到这一点:

int n = GetRandomIndex();
object item = dictionary.ElementAt(n).Value;

Linq could do this for you:

int n = GetRandomIndex();
object item = dictionary.ElementAt(n).Value;
情归归情 2024-08-01 15:05:50

您没有提供足够的信息来提出解决方案。 有多少个元素,您打算多久执行一次此操作,您是否有内存/速度限制? BTree、SortedList、在 SortedDictionary 中插入特殊节点都可能有用

You don't provide enough information to come up with a solution. How many elements, how often are you going to do this, do you have memory/speed constraints? BTree, SortedList, inserting special nodes in the SortedDictionary could all be useful

花辞树 2024-08-01 15:05:50

提取随机密钥有效吗?

var randValue = myDictionary.Values.ToList()[myRandomInt];

编辑:

似乎键集合和值集合都是 IEnumerables,因此您不能使用 [] 运算符。 这似乎是最好的。

编辑:

没有 Linq...也许很昂贵,但您可以复制到数组,然后在索引处提取值

System.Collections.Generic.KeyValuePair<string, int>[] dictCopy = new System.Collections.Generic.KeyValuePair<string, int>[myDictionary.Count];
myDictionary.CopyTo(dictCopy, 0);
var randValue = dictCopy[myRandomInt].Value;

Will pulling a random key work?

var randValue = myDictionary.Values.ToList()[myRandomInt];

Edit:

Seems the keys collection and values collection are both IEnumerables so you can't use [] operators. This is the best it gets it seems.

Edit:

Without Linq... Perhaps expensive, but you could copyto array and then pull a value at an index

System.Collections.Generic.KeyValuePair<string, int>[] dictCopy = new System.Collections.Generic.KeyValuePair<string, int>[myDictionary.Count];
myDictionary.CopyTo(dictCopy, 0);
var randValue = dictCopy[myRandomInt].Value;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文