在sortedlist队列中,queue.value [0]给出min键的对应值.如果我想让它给出最大键值?
我必须重写icomparer吗?
我想实现一个优先级队列,它将注入我的对象 - Nodes相对于一个字段的队列 - f.我已经用自定义比较器编写了List,但这需要我:
dequeue - 删除最后一个(而不是第一个表现),如此
myList.RemoveAt(myList.Count - 1);
Run Code Online (Sandbox Code Playgroud)我的列表应该总是根据某个字段排序(这里我需要它按照排序f).我还需要能够添加dequeue列表中具有最低值的对象.
有人能告诉我最好的办法是什么?
编辑
dasblinkenlight有一个非常好的答案,但我已经意识到我应该能够在这个容器中存储重复项.
.NET附带了一些内置集合(堆栈,队列,字典,列表等),但缺少其他常见集合,如优先级队列.在NuGet上有很多第三方收集库,但我想知道是否有一个正式的Microsoft(如现在称为BCL不可变集合System.Collections.Immutable)库,其中包含(可变的)优先级队列?
编辑:重要澄清(@rmunn评论):
这个问题并不是要求为X推荐一个库,而是在问一个事实问题,"X是否有官方的Microsoft库?"
我想要一个集合,将浮点数的键值对存储到整数(浮点数是关键).然后我想找到具有最小数字的键值对.所以,我基本上想要使用最低的关联浮点值来获取int值.
也许是一个集合,它根据键保持它们的顺序,并允许我索引它,以便在索引0抓取对象是合适的?我不知道从哪里开始寻找这个.
我有一个struct包含一些int和bool成员的,我希望从列表中获得最低值(实际上是基于A*搜索的路径查找器).
基本上,我的对象看起来像这样:
public struct Tile
{
public int id;
public int x;
public int y;
public int cost;
public bool walkable;
public int distanceLeft;
public int parentid;
}
Run Code Online (Sandbox Code Playgroud)
我想得到距离最低的物品.列表声明如下:
List<Structs.Tile> openList = new List<Structs.Tile>();
Run Code Online (Sandbox Code Playgroud)
并以这种方式分配值:
while (pathFound == null)
{
foreach (Structs.Tile tile in map)
{
foreach (Structs.Tile tile1 in getSurroundingTiles(Current))
{
if (tile1.x == tile.x && tile1.y == tile.y)
{
Structs.Tile curTile = tile1;
curTile.parentid = Current.id;
curTile.distanceLeft = (Math.Abs(tile.x - goalx) + …Run Code Online (Sandbox Code Playgroud)