我需要在我的 sortedDictionary 中设置一个元素的值,通过索引访问。
IE
sortedDictionary.Values[index] = value; // compile error
请注意,以下内容不正确,因为它是通过键而不是索引访问的。
sortedDictionary[index] = value; // incorrect
我想出了以下解决方案,但直觉告诉我它很慢。我假设按键访问是O(log N),索引访问是O(1),但我不确定。
sortedDictionary[sortedDictionary.ElementAt(index).Key] = value;
一些背景:
我使用 SortedDictionary 是因为我需要快速插入、删除、查找以及能够访问相邻元素。(即次高或次低。)效率很重要。