移植C++;Java的排序函数 我把C++代码移植到java上有问题。我有一个配对的清单如下 vector<pair<pair<int,int>,int> > M; List<Pair<Pair<Integer,Integer>,Integer>> pairList = new ArrayList<>(); sort(M.begin(), M.end()); class Pair<K,V>{ K k; V v; public void makePair(K k, V v){ this.k = k; this.v = v; } }
这是我的问题,我需要用Java编写的等效比较器是什么? 我如何使用它?我假设有以下几行移植C++;Java的排序函数 我把C++代码移植到java上有问题。我有一个配对的清单如下 vector<pair<pair<int,int>,int> > M; List<Pair<Pair<Integer,Integer>,Integer>> pairList = new ArrayList<>(); sort(M.begin(), M.end()); class Pair<K,V>{ K k; V v; public void makePair(K k, V v){ this.k = k; this.v = v; } },java,Java,这是我的问题,我需要用Java编写的等效比较器是什么? 我如何使用它?我假设有以下几行 Collections.sort(pairList, new MyComparator()) 有人能帮我理解什么是MyCompartor吗 结对类如下 vector<pair<pair<int,int>,int> > M; List<Pair<Pair<Integer,Integer>,Integer>> pairList = new
Collections.sort(pairList, new MyComparator())
有人能帮我理解什么是MyCompartor吗
结对类如下
vector<pair<pair<int,int>,int> > M;
List<Pair<Pair<Integer,Integer>,Integer>> pairList = new ArrayList<>();
sort(M.begin(), M.end());
class Pair<K,V>{
K k;
V v;
public void makePair(K k, V v){
this.k = k;
this.v = v;
}
}
类对{
K;
V V;
公共无效生成对(kk,vv){
这个。k=k;
这个,v=v;
}
}
解决方案
我最终实现了MyComparator,如下所示
static class MyComparator implements Comparator<Pair<Pair<Integer,Integer>,Integer>>{
@Override
public int compare(Pair<Pair<Integer,Integer>,Integer> p1,Pair<Pair<Integer,Integer>,Integer> p2){
int a = p1.k.v.compareTo(p2.k.v);
if(a==0) return a;
else return p1.k.k.compareTo(p2.k.k);
}
}
静态类MyComparator实现Comparator{
@凌驾
公共整数比较(对p1,对p2){
INTA=p1.k.v.与p2.k.v.相比;
如果(a==0),则返回a;
否则返回p1.k.k.compareTo(p2.k.k);
}
}
谢谢大家。您的比较器应该是一个实现接口
比较器的类。T是与集合关联的泛型类型
对你来说,这是一对
所以你应该有这样一节课:
公共类MyComparator实现Comparator{
@凌驾
公共整数比较(对o1,对o2){
//您应该在此处输入比较代码:
//如果两个对象相同,则返回0
//如果o2>o1,则返回-1(或另一个负整数)
//如果o2
MyComparator
需要实现,以便Java知道如何比较列表中的元素。可以采用两种形式比较器
或
<> P.>你可以通过使java <代码>配对> /Cuff>类像C++一样来避免比较器。ApacheCommonsLang有一个可以满足您需要的功能
但是:为什么不使用适当的自定义数据类而不是嵌套对呢?我已经认为C++版本相当麻烦。
如果您不想使用它,请使用您自己的:
class Pair<K extends Comparable<K>,V extends Comparable<V>>
implements Comparable<Pair<K, V>>
{
// have a immutable pairs
final K k;
final V v;
Pair(final K k, final V v)
{
this.k = k;
this.v = v;
}
// factory function, the same as in C++
// (where it exists because template syntax is akward)
// (as is generics syntax)
public static <K extends Comparable<K>, V extends Comparable<V>> Pair<K, V> makePair(K k, V v)
{
return new Pair(k, v);
}
@override
public int compareTo(final Pair<K, V> other)
{
int compare = this.k.compareTo(other.k);
if (compare == 0) {
compare = this.v.compareTo(other.v);
}
return compare;
}
// NOTE: we also need equals() and hashCode() for a proper class.
}
类对
实现可比性
{
//有一对不变的
最终K;
最后V V;
配对(最终KK,最终V)
{
这个。k=k;
这个,v=v;
}
//工厂函数,与C++相同
//(由于模板语法为akward而存在)
//(与泛型语法一样)
公共静态对makePair(K,V)
{
返回新的对(k,v);
}
@凌驾
公共整数比较(最后一对其他)
{
int compare=this.k.compareTo(other.k);
如果(比较==0){
比较=此.v.compareTo(其他.v);
}
返回比较;
}
//注意:对于适当的类,我们还需要equals()和hashCode()。
}
我会尽量避免编写隐式的比较器
并将其传递给sort函数,除非您需要特殊的大小写排序。Java端对
来自哪里(它不是标准)?如果它确实与字典(字典中的C++对)比较,则不需要比较器。小心的复制品,你的修复方法应该是一个带有名字对的构造函数。看起来你在滥用对,所以你应该创建新的结构,而不是这个代码>三倍<代码>。谢谢。我正要像你那样插入Javadoc的链接。