set

Is it possible to change the comparator of a C++ std::set?

你离开我真会死。 提交于 2019-12-10 14:48:13
问题 I have a set of data which in some occasion I need to sort them in one way and some occasion in another way. For example, suppose the data set is a set of strings,{"abc", "dfg",...}. Sometimes I need to sort them in alphabetic order and sometimes by comparing their length. Initially I used std::set as a container of my data and implemented 2 comparators, hoping that I can change the comparator of the set on the fly, cause the data is huge and it's not a good idea to copy it from one set to

Strange error, set<int>::begin() always returning const iterator

笑着哭i 提交于 2019-12-10 14:24:01
问题 Why is set.begin() always returning a const iterator and not a standard one? 35 int test(){ 36 std::set<int> myset; 37 myset.insert(2); 38 myset.insert(3); 39 int &res = *myset.begin(); 40 return res; 41 } test.cpp:39: error: invalid initialization of reference of type ‘int&’ from expression of type ‘const int’ 回答1: It's not returning a const_iterator , rather the key_type of std::set<int> is const int . Remember that keys in a std::set are constant. You can't change a key after it's inserted

.NET check if two IEnumerable<T> have the same elements [duplicate]

痴心易碎 提交于 2019-12-10 14:12:54
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: Comparing two collections for equality I need to verify if two IEnumerable<T> lists have the same elements, not necessarily in the same order. I'm targetting .NET 3.5. Here are the tests. The question is, how should HasSameElements() be implemented? var l1 = new[]{1,2,3}; var l2 = new[]{3,1,2}; bool rez1 = l1.HasSameElements(l2);//should be true var l3 = new[]{1,2,3,2}; var l4 = new[]{3,1,2,2}; bool rez2 = l3

Can i have HashSets as the keys in a HashMap? Suggest an alternative if not

安稳与你 提交于 2019-12-10 13:45:24
问题 Edit: explained the problem properly now. I have a hashmap where i want to store sets of words seen together (key) and the lines in which they were seen together(value). This is the structure i came up with: HashMap<HashSet<String>, HashSet<Integer>> hm= ... for inputs: mango, banana, apple apple, banana peach, walrus walrus, peach As I read this, line by line, I make new temporary keys (hashsets not yet inserted into hashmap) from the combination of words in the line. Each temporary key is a

How to override a related set's “add” method in Django

浪子不回头ぞ 提交于 2019-12-10 13:37:59
问题 I am working on a Django project and I want to send a signal when something gets added to some model's related set. E.g. we have an owner who has a set of collectables, and each time the method owner.collectable_set.add(something) is getting called, I want a signal like collectable_added or something. Signals are clear to me, but I don't know which manager(?) contains the "add" method that I want to override. Edit for Xavier's request to provide more details: you can easily override a model’s

Checking if a Set contains a list in Javascript

谁说胖子不能爱 提交于 2019-12-10 13:37:53
问题 I have a set that I've added a list to. var a = new Set(); a.add([1]); Now I check if [1] exists in a : a.has([1]); > false Based on the spec, this might be happening because if type is same for two objects being compared: Return true if x and y refer to the same object. Otherwise, return false. And this also happens: [1] == [1]; > false So it might be that the Set .has() is using == for comparing equality (not sure though). Is there a .contains() method for Set() in JS like Java has? 回答1:

Python: How to allow duplicates in a set?

萝らか妹 提交于 2019-12-10 13:16:56
问题 I ran into a problem regarding set in Python 2.7. Here's the appropriate example code block: letters = set(str(raw_input("Type letters: "))) As you can see, the point is to write some letters to assign to "letters" for later use. But if I type "aaabbcdd", the output of "letters" returns set(['a', 'c', 'b', 'd']) My question is how to write the code, so that the output will allow duplicates like this: set(['a','a','a','b','b','c','d','d']) ? 回答1: set doesn't store duplicates, which is why it's

In python - the operator which a set uses for test if an object is in the set

女生的网名这么多〃 提交于 2019-12-10 12:39:32
问题 If I have a list of objects, I can use the __cmp__ method to override objects are compared. This affects how the == operator works, and the item in list function. However, it doesn't seem to affect the item in set function - I'm wondering how I can change the MyClass object so that I can override the behaviour how the set compares items. For example, I would like to create an object that returns True in the three print statements at the bottom. At the moment, the last print statement returns

Hibernate Set Or List

送分小仙女□ 提交于 2019-12-10 12:34:54
问题 Can anybody know when to use Set and when to use List in hibernate mapping file ? <set name="" table=""> <key> <column name="" /> </key> </set> <list name="" cascade="all"> <key column="" /> <index column="" /> <one-to-many class=""/> </list> Thanks. 回答1: Take a look at this post: @OneToMany List<> vs Set<> difference The main difference is that a list has ordering while a set does not. Also, a set cannot have duplicate values, while a list can. 来源: https://stackoverflow.com/questions/9188718

Performance of Guava's ImmutableSet.contains

五迷三道 提交于 2019-12-10 12:32:36
问题 Guava's ImmutableSet seems to perform quite poorly in my benchmark concerning contains . For some sizes it gets even much slower than List : size benchmark ns linear runtime 100000 ListContains 110279.54 == 100000 SetContains 7.15 = 100000 ImmutableSetContains 76716.47 = 200000 ListContains 275367.66 ===== 200000 SetContains 7.34 = 200000 ImmutableSetContains 322185.50 ====== 500000 ListContains 935210.10 ==================== 500000 SetContains 7.79 = 500000 ImmutableSetContains 1382765.76 ==