有没有在C#中的非唯一键排序列表泛型集合? [英] Is there a non-unique-key sorted list generic collection in C#?

查看:72
本文介绍了有没有在C#中的非唯一键排序列表泛型集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是有点惊讶System.Collections.Generic.SortedList,在

I'm a bit surprised by System.Collections.Generic.SortedList, in that


  1. 这要求我使用<关键字,值> 而不是<价值> (比较器)

  2. 这只允许每值项

  1. It requires me to use <key, value> instead of <value>(comparer)
  2. It only allows on entry per value

这些似乎古怪的方式,我想用它(虽然我敢肯定,他们只是适合其他情况)。是否有不具备这两个特点

These seem quirky in the way I want to use it (although I'm sure they're just right for other situations). Is there another collection that doesn't have these two characteristics?

推荐答案

排序列表<另一个集合;,> 是真的键排序的映射,而不是一个列表。不好的命名,也许。但也有办法来模拟你想要什么,根据您的具体要求。你可以,例如,封装了排序列表< T,INT> 并添加/删除是这样的:

SortedList<,> is really a map sorted by key, not a list. Bad naming, maybe. But there are ways to emulate what you want, depending on your exact requirements. You could, for example, encapsulate a SortedList<T, int> and have add/remove something like:

// add
int count;
if(list.TryGetValue(value, out count)) list[value] = count+1;
else list[value] = 1;



最后你可以使用一个简单的列表(列表<> )太 - 这取决于你在做什么

Ultimately you could use a simple list (List<>) too - it depends what you are doing.

在某种程度上,我希望数据绑定等等使得它很难实施立即排序常规列表 - 你需要实现的很多的接口来获取工作,因为通常它希望你添加到留底的项目

In part, I expect that data-binding etc makes it hard to implement a regular list that sorts immediately - you need to implement a lot of interfaces to get that working, as normally it expects the item you add to stay at the end.

这篇关于有没有在C#中的非唯一键排序列表泛型集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆