comparator

How can I search an std::map using a key of a different type

ぐ巨炮叔叔 提交于 2019-12-01 15:10:15
问题 If I have std::map<X, Blah> , what is the best way of looking up a matching item in the map using an instance of Y ? Assume the information in Y is enough to uniquely find an X , but for performance reasons I don't want to create an instance of X by copying Y values. I realize I can do this by creating a common base class or interface for X and Y and making that the map key, but is there any other way? e.g. creating some sort of comparator object? Here is the sample code for clarity: class X

Comparable与Comparator的区别

时光怂恿深爱的人放手 提交于 2019-12-01 12:19:12
前几天在项目中遇到了一个将复杂对象进行排序的问题:计算BingMap地图上距离 当前位置5KM内 发生事故(TrafficIncident)的点到当前位置的距离,并按距离升序排序。距离都算出来了,但这些TrafficIncident对象的排序却难到了我。经同事提醒,Comparable或Comparator是一个不错的选择。于是在网上搜索了一些资料,总结下来。 方式一: 实现Comparable接口 Comparable是java.lang包下的一个接口,该接口里只有一个compareTo()方法: package java.lang; import java.util.*; public interface Comparable<T> { public int compareTo(T o); } Comparable翻译为“可比较的”,表明实现该接口的类都是可以比较的,即实现Comparable接口的类本身就已经支持自比较,例如: String、Integer 自己就可以完成比较大小操作,它们已经实现了Comparable接口。查看String类的源码可以看见是这样声明的(JDK1.7): public final class String implements java.io.Serializable, Comparable<String>, CharSequence

Bound mismatch for java Collections sorting

泄露秘密 提交于 2019-12-01 12:08:53
问题 Hi need Help regarding java collection sorting. It gives me this error: Bound mismatch: The generic method sort(List<T>) of type Collections is not applicable for the arguments (List<WifiSSID>). The inferred type WifiSSID is not a valid substitute for the bounded parameter <T extends Comparable<? super T>> My code looks like: public class WifiSSID { public String SSIS; public double id; } public class ScanFilterWifiList { public ScanFilterWifiList(List<WifiSSID> wifiList) { Collections.sort

Java 8 Comparator keyExtractor [duplicate]

馋奶兔 提交于 2019-12-01 11:58:13
This question already has an answer here: Comparator.comparing(…) of a nested field 3 answers In Java 8 Comparator, we can create a comparator as follows. Comparator.comparing(keyExtractor); Currently I have a class as follows class Employee { String name; Department dept; } class Department { String departmentName; } Now, if I want to create a comparator for Employee class which sorts the records based on the department name, how can I write my key extractor? Tried the below code, but did not work. Comparator.comparing(Employee::getDept::getDepartmentName); The trick here is method references

C++ Dynamically assign std::map comparator

喜你入骨 提交于 2019-12-01 11:44:53
So I have two classes containing std::map members with effectively identical functionality except that the ordering of one map is std::less and the other std::greater. If I create an abstract parent class and declare a single map member, is there any way to dynamically assign the comparator for this member in the derived class constructors? That way the functionality can obviously all reside in the parent class. You can't change the comparator after the fact. But you can use the same comparator class and get either "greater" or "less" at the time of construction. You just need a stateful

Java 8 Comparator keyExtractor [duplicate]

两盒软妹~` 提交于 2019-12-01 11:29:14
问题 This question already has answers here : Comparator.comparing(…) of a nested field (3 answers) Closed last year . In Java 8 Comparator, we can create a comparator as follows. Comparator.comparing(keyExtractor); Currently I have a class as follows class Employee { String name; Department dept; } class Department { String departmentName; } Now, if I want to create a comparator for Employee class which sorts the records based on the department name, how can I write my key extractor? Tried the

How to create a std::set with custom comparator in C++?

孤者浪人 提交于 2019-12-01 10:34:57
问题 How do I create a set of pairs, the elements of which (the pairs) are sorted with a custom bool function? I write set <pair<int,int>,compare> myset; and get error : Type/value mismatch at argument 2, expected a type, got "compare" I have defined "compare" as bool compare(pair <int,int> g1, pair <int,int> g2) { return (g1.second-g1.first > g2.second-g2.first); } and of course #include <vector> #include <set> 回答1: Method 1: use functor Write a class that overloads the operator() so it can be

Collections.sort() using comparator? [closed]

不羁岁月 提交于 2019-12-01 08:05:46
import java.util.*; public class C_2 { public static void main(String args[]) { String theStrings[] = { "x", "a", "b", "c", "d" }; List l = Arrays.asList(theStrings); Collections.sort(l); // line a Collections.sort(l, new ThisIsMyThing()); // line b System.out.println(l); } } class ThisIsMyThing implements Comparator { public int compare(Object o1, Object o2) { String s1 = (String)o1; String s2 = (String)o2; return -1 * s1.compareTo(s2); } } I understand that class C_2 does sorting based on two different techniques. One is the standard Collections.sort(l); And the other is Collections.sort(l

Collections.sort() using comparator? [closed]

余生长醉 提交于 2019-12-01 04:20:27
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 6 years ago . import java.util.*; public class C_2 { public static void main(String args[]) { String theStrings[] = { "x", "a", "b", "c", "d" }; List l = Arrays.asList(theStrings); Collections.sort(l); // line a Collections.sort(l, new ThisIsMyThing()); // line b System.out.println(l); } } class ThisIsMyThing

Misunderstanding about Comparator in java 8

别等时光非礼了梦想. 提交于 2019-12-01 02:54:28
public class Test { public static void main(String[] args) { List<Pair<String, Integer>> list = new ArrayList<>(); list.add(new Pair<>("1", 8)); list.add(new Pair<>("3", 2)); list.add(new Pair<>("2", 15)); list.stream() .sorted(Comparator.comparingInt(p -> p.v)) .map(p -> p.k) .forEach(System.out::println); } } class Pair<K, V> { K k; V v; public Pair(K k, V v) { this.k = k; this.v = v; } } Ok, as you understood this code is printing my pair keys from the lowest value associated to the highest so I get the expected output: 3 1 2 So far so good. Now I wanted to do the reverse, I thought I would