快速/有效的方式来得到的名单,其中最小值指数; T&GT;? [英] Fast/efficient way to get index of minimum value in List<T>?
本文介绍了快速/有效的方式来得到的名单,其中最小值指数; T&GT;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法找到最小值指数更高效/比这个更快?
INT minimumValueIndex = List.IndexOf(List.Min());
解决方案
是的,你可以通过建立一个自定义删除的 List.IndexOf()
的开销 MIN()
扩展。 (真的, Enumerable.Min()
应该有选择的延伸的原的按键,而不是选择一个转换的元素。这监督尤其痛苦在这样的情况下。)
公共静态INT IndexOfMin(这个IList的&LT; INT&GT;个体经营)
{
如果(个体经营== NULL){
抛出新ArgumentNullException(自我);
}
如果(self.Count == 0){
抛出新的ArgumentException(列表是空的。,自我);
}
INT分钟=自[0];
INT minIndex = 0;
的for(int i = 1; I&LT; self.Count ++我){
如果(自[1] - ;分){
分=自[I]
minIndex =我;
}
}
返回minIndex;
}
Is there any way to find minimum value index more efficient/faster than this?
int minimumValueIndex = List.IndexOf(List.Min());
解决方案
Yes, you can remove the overhead of List.IndexOf()
by building a custom Min()
extension. (Really, Enumerable.Min()
should have an extension that selects the original element by key instead of selecting a transformation. This oversight is particularly painful in situations like this.)
public static int IndexOfMin(this IList<int> self)
{
if (self == null) {
throw new ArgumentNullException("self");
}
if (self.Count == 0) {
throw new ArgumentException("List is empty.", "self");
}
int min = self[0];
int minIndex = 0;
for (int i = 1; i < self.Count; ++i) {
if (self[i] < min) {
min = self[i];
minIndex = i;
}
}
return minIndex;
}
这篇关于快速/有效的方式来得到的名单,其中最小值指数; T&GT;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文