comparator

How can I set two kind of comparator (one for insert, one for find) on this multiset?

蹲街弑〆低调 提交于 2019-12-12 06:36:17
问题 I have declared this STL multiset : multiset<IMidiMsgExt, IMidiMsgExtComp> playingNotes; and my comparator is: struct IMidiMsgExtComp { bool operator()(const IMidiMsgExt& lhs, const IMidiMsgExt& rhs) { return lhs.mTick < rhs.mTick; } }; and this serves to me well on .insert : playingNotes.insert(midiMessage); it inserts (and than orders) the item having the min mTick at the top and the max mTick in the bottom of the list. So its ordered by mTick , and every .begin() will return a IMidiMsgExt

How to Sort a List of Maps by two Categories?

℡╲_俬逩灬. 提交于 2019-12-12 03:14:14
问题 I have a List of maps, in which I have two categories "foo" and "bar"! With my function now, I can Iterate it and sort by foo with this: public List<Map<String, Object>> makeAllSkillsMaps(List<Stuff> li) { List<Map<String, Object>> l = new LinkedList<Map<String, Object>>(); List<Skill> w_l = li; for (Stuff stuff : w_l) { Map<String, Object> r_m = new HashMap<String, Object>(); r_m.put("ID", stuff.getID()); r_m.put("Bar", stuff.getBar()); r_m.put("Foo", stuff.getFoo()); l.add(r_m); }

map Comparison Constructor Parameter

若如初见. 提交于 2019-12-12 03:02:40
问题 Is there a reason why I cannot pass a comparison functor to a map as a constructor argument: map<int, string, greater<int>> foo; //map<int, string> foo(greater<int>()); Doesn't work Or why I cannot pass a lambda without providing my own comparison type: map<int, string, function<bool(const int&, const int&)>> bar([](const int& lhs, const int& rhs){ return lhs > rhs; }); //map<int, string> bar([](const int& lhs, const int& rhs){ return lhs > rhs; }); Doesn't work I'd like to just be able to

Use of Comparator interface Java - Sort files

﹥>﹥吖頭↗ 提交于 2019-12-12 01:48:54
问题 I have a program to sort files in a certain directory of the computer. I am using the Comparator-interface and using the collections.sort-method but I cannot access the output - from the calling-class. I neither know how to sort the objects in the Sort-class either. 1) Would be glad if someone could tell how I use the compare-method (prototyp is: sort(List list, Comparator c) 2) How do I get the output in the directory class? Because Sort-class is parameterisized I cannot access the method

Overloading the comparison operators == C++

笑着哭i 提交于 2019-12-11 18:31:19
问题 I have a base class Person with 3 instance vars. Person(string name, unsigned long id, string email) and one derived class Student that inherits Person and have one new instance var year Student(string name, unsigned long id,int year,string email): Person(name,id,email) and one class Teacher that isn't necessary to describe. Then a have the class named eClass and I want Overload the comparison operator == and use that operator in function bool exists() when I compile my .cpp i have that error

Drawbacks of not returning 0 when overriding 'compare()' in Java 1.5 and Java 1.7

六眼飞鱼酱① 提交于 2019-12-11 18:28:13
问题 This is the comparator I wrote to to sort Nodes based on cost. public class MyCostComparator implements Comparator<Node>{ public int compare(Node a, Node b){ if(a.pathCost > b.pathCost) return 1; else return -1; } } I find that it's behaviour is different on my machine (Java 1.7) and on the Uni's server (Java 1.5). However when I make it: if(a.pathCost >= b.pathCost) , it seems to work fine on 1.5, but the other way on 1.7. Also, what's the drawback of NOT returning zero when the values are

Merge n lists and sort data maintaining original order/constraint

瘦欲@ 提交于 2019-12-11 07:56:20
问题 I encountered the below problem in a coding competition. I tried a lot but a private test case was always failing for me with wrong answer and I am unable to figure out why my below approach would fail. I've no naive solution to generate stress test cases and compare. Also, there will be no editorial published. So, I am looking for someone to point out the flaw in my approach if possible. Following is a detailed description of the problem and what I've tried till now. Problem: There are

Python Find minimum object using special comparator

坚强是说给别人听的谎言 提交于 2019-12-11 05:55:46
问题 So I got my question answered on how to find the minimum value, but what if I want the minimum object? Can we similarly simplify this code? min = 9999 minChild = None for child in currentRoot.children: if child.score < min: min = child.score minChild = child recurseWriteBook(child, depth+1) 回答1: Use built-in function min(iterable[, key]): >>> class Foo(object): ... def __init__(self, value): ... self.value = value ... >>> >>> l = [Foo(2), Foo(1), Foo(3)] >>> >>> >>> min_foo = min(l, key =

How to search in a Set (using a Comparator)

拥有回忆 提交于 2019-12-11 04:24:11
问题 I want to search in a Set without iterating manually over the elments but there does not seem to be a method to do Collections.search(myset, target, new ComparatorThing()). Am I not seeing something? Thanks. Edit: I am searching for another field than the natural order of the elements. As a manual workaround I used the following static method. Should okay, since you can't make any assumtions about the other using a custom field in the comparator anyways. public static T search(final Set set,

Identity operation equivalent for Comparator

青春壹個敷衍的年華 提交于 2019-12-11 04:08:57
问题 Is there a possible identity representation of Comparator that could exist? In the search for simplifying the code in Removing overloaded method in Java, I thought about this and ended up concluding that if every comparison results in objects being equal, the order wouldn't really change making the operation an identity . Hence I ended up with (an inefficient) suggestion such as this: public static <T, G> List<G> toListOfNewType(List<T> inputList, Function<T, G> mapperFunction) { return