gpt4 book ai didi

c# - 快速插入类链接对象

转载 作者:行者123 更新时间:2023-11-30 14:55:47 25 4
gpt4 key购买 nike

在基于代理的实时建模项目中,我遇到以下问题:假设我有一些带有公共(public)属性 X(X 是 double )的 MyClass 类。我需要一个可以按排序方式包含我的对象的类(称之为 FancyList)。如果我插入一个新的 MyClass 对象,它应该插入到具有较低 X 的对象和具有较高 X 的对象之间。

FancyList _fancyList = new FancyList();

// Allocate MyClass objects. The X proporty is defined in the constructor
MyClass c1 = new MyClass(2.0)
MyClass c2 = new MyClass(3.0)
MyClass c3 = new MyClass(2.5)

_fancyList.Insert(c1);
_fancyList.Insert(c2);
_fancyList.Insert(c3);

// In the fancyList c3 is after c1 and before c2. Therefore c1 is the first
// element, and c2 is the last element

尽可能快地插入很重要。

最佳答案

使用 SortedList<>SortedDictionary<> .

他们都实现了IDictionary .根据the MSDN docs SortedDictionary 具有更快的插入操作。一定要阅读这些规范的其余部分。

FancyList _fancyList = new SortedDictionar<double, MyClass>();

// Allocate MyClass objects. The X proporty is defined in the constructor
...

_fancyList.Insert(c1.X, c1);
_fancyList.Insert(c2.X, c2);
_fancyList.Insert(c3.X, c3);

关于c# - 快速插入类链接对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24587035/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com