C#中的优先级不使用自定义比较
有人可以帮助我理解为什么这不起作用以及如何使其起作用?
PriorityQueue<Candidate, int> pq =
new PriorityQueue<Candidate, int>(new EstimateCompare());
public class EstimateCompare: IComparer<Candidate>
{
public int Compare(Candidate a, Candidate b)
{
int diff = a.estimateDistance -b.estimateDistance;
return diff == 0 ? 0 : diff < 0 ? 1 : -1;
}
}
这是我遇到的错误:
CS0535:“ estectecompare”不实现接口成员 'icomparer.compare(int,int)'(在解决方案中)
尝试使用它,我得到此错误:
PriorityQueue<Candidate, int> pq =
new PriorityQueue<Candidate, int>(new EstimateCompare());
public class EstimateCompare: IComparer<Candidate> {
public int Compare(Candidate a, Candidate b) {
return a.estimateDistance.CompareTo(b.estimateDistance);
}
错误CS1503:参数1:无法从“ estectecompare”转换为 'system.collections.generic.icomparer?'
公共类候选人{ int行; int col; int距离; 公共INT估计含量; 公共候选人(int row,int col,int int distancesofar,int估计){ this.row = row; this.col = col; this.distancesofar = distancesofar; this.Estimatedistance =估计值; } }
非常感谢!!
Can someone help me understand why this doesn't work and how can make it work?
PriorityQueue<Candidate, int> pq =
new PriorityQueue<Candidate, int>(new EstimateCompare());
public class EstimateCompare: IComparer<Candidate>
{
public int Compare(Candidate a, Candidate b)
{
int diff = a.estimateDistance -b.estimateDistance;
return diff == 0 ? 0 : diff < 0 ? 1 : -1;
}
}
Here is the error I get:
CS0535: 'EstimateCompare' does not implement interface member
'IComparer.Compare(int, int)' (in Solution.cs)
Tried using it like this and I get this error:
PriorityQueue<Candidate, int> pq =
new PriorityQueue<Candidate, int>(new EstimateCompare());
public class EstimateCompare: IComparer<Candidate> {
public int Compare(Candidate a, Candidate b) {
return a.estimateDistance.CompareTo(b.estimateDistance);
}
error CS1503: Argument 1: cannot convert from 'EstimateCompare' to
'System.Collections.Generic.IComparer?'public class Candidate { int row; int col; int distanceSoFar; public int estimateDistance; public Candidate(int row, int col, int distanceSoFar, int estimateDistance) { this.row = row; this.col = col; this.distanceSoFar = distanceSoFar; this.estimateDistance = estimateDistance; } }
Many Thanks!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在使用的构造函数是
priordityqueue&lt;远程代码>
,也就是说,
telement
是候选人
,tpriority
isint
,因此参数应该是icomparer&lt; int&gt;
的实例,但是estectAteCompare
是icomparer&lt; cant&gt;
。So for example, the example compiles if you instead used
But given what your implementation of
Compare
looks like, you probably don't want anIComparer
at all, and you could instead do with yourPriorityQueue
and just use, saywhen adding elements to the queue.
The constructor you're using is
PriorityQueue<TElement, TPriority>(IComparer<TPriority>)
, that is,TElement
isCandidate
, andTPriority
isint
, so the argument should be an instance ofIComparer<int>
, butEstimateCompare
is anIComparer<Candidate>
.So for example, the example compiles if you instead used
But given what your implementation of
Compare
looks like, you probably don't want anIComparer
at all, and you could instead do with yourPriorityQueue<Candidate, int>
and just use, saywhen adding elements to the queue.