unordered-set

How to use unordered_set with compare function?

 ̄綄美尐妖づ 提交于 2019-12-11 12:48:06
问题 I wrote my own compare function for the third template parameter of std::unorderd_set. My function is static bool HasSamePosition(const Node& a, const Node& b); in the class Node. Now I'm trying to use this function in my unordered set, std::unordered_set<Node, std::hash<Node>, bool(*)(const Node& a, const Node& b)> closedlist(&Node::HasSamePosition); but it doesn't work. The error ist, that no instance of the constructor is matching the argumentlist. What am I missing? 回答1: Well the compiler

Variant of the 2-sum algorithm with a range of sums

≯℡__Kan透↙ 提交于 2019-12-11 05:34:13
问题 The problem statement is as follows: The goal of this problem is to implement a variant of the 2-SUM algorithm. The file contains 1 million integers, both positive and negative (there might be some repetitions!).This is your array of integers, with the ith row of the file specifying the ith entry of the array. Your task is to compute the number of target values t in the interval [-10000,10000] (inclusive) such that there are distinct numbers x,y in the input file that satisfy x+y=t. Write

std::string_view and std::string in std::unordered_set [duplicate]

佐手、 提交于 2019-12-10 21:43:10
问题 This question already has an answer here : C++ unordered_map<string, …> lookup without constructing string (1 answer) Closed last year . Let's say you have an std::unordered_set<std::string> . You have an std::string_view object that you want to search for in the container. Problem is, you don't want to create a std::string from your std::string_view , as this kind of defeats the purpose of using std::string_view in the first place. However, it seems that std::string_view should be usable as

adding elements of a vector to an unordered set

孤者浪人 提交于 2019-12-09 14:05:41
问题 Is there an easy way to add all the elements of a vector to an unordered_set ? They are of the same type. Right now, I am using a for loop and was wondering if there is a better way to do it 回答1: If you're constructing the unordered_set then: std::vector<int> v; std::unordered_set<int> s(v.begin(), v.end()); 回答2: Forgive me if my syntax has any minor bugs, but you can try the std::copy function, its meant for this purpose. std::vector<int> v; std::unordered_set<int> s; std::copy(v.begin(),v

why hastable's rehash complexity may be quadratic in worst case

只谈情不闲聊 提交于 2019-12-07 15:46:46
问题 I do not understand why hastable's rehash complexity may be quadratic in worst case at : http://www.cplusplus.com/reference/unordered_set/unordered_multiset/reserve/ Any help would be appreciated ! Thanks 回答1: Just some basics: Hash collisions is when two or more elements take on the same hash. This can cause worst-case O(n) operations. I won't really go into this much further, since one can find many explanations of this. Basically all the elements can have the same hash, thus you'll have

tr1::hash for boost::thread::id?

北城余情 提交于 2019-12-06 21:15:06
问题 I started to use the unordered_set class from the tr1 namespace to speed-up access against the plain (tree-based) STL map . However, I wanted to store references to threads ID in boost ( boost::thread::id ), and realized that the API of those identifiers is so opaque that you cannot clearly obtain a hash of it. Surprisingly, boost implements parts of the tr1 (including hash and unordered_set ), but it does not define a hash class that is able to hash a thread ID. Looking at the documentation

why hastable's rehash complexity may be quadratic in worst case

限于喜欢 提交于 2019-12-06 01:59:23
I do not understand why hastable's rehash complexity may be quadratic in worst case at : http://www.cplusplus.com/reference/unordered_set/unordered_multiset/reserve/ Any help would be appreciated ! Thanks Just some basics: Hash collisions is when two or more elements take on the same hash. This can cause worst-case O(n) operations. I won't really go into this much further, since one can find many explanations of this. Basically all the elements can have the same hash, thus you'll have one big linked-list at that hash containing all your elements (and search on a linked-list is of course O(n) )

Using a std::unordered_set of std::unique_ptr

狂风中的少年 提交于 2019-12-05 08:58:37
问题 Assume I have a set of unique_ptr: std::unordered_set <std::unique_ptr <MyClass>> my_set; I'm not sure what's the safe way to check if a given pointer exists in the set. The normal way to do it may be to call my_set.find () , but what do I pass as a parameter? All I have from the outside is a raw pointer. So I have to create another unique_ptr from the pointer, pass it to find() and then release() that pointer, otherwise the object would get destructed (twice). Of course, this process can be

tr1::hash for boost::thread::id?

做~自己de王妃 提交于 2019-12-05 02:08:44
I started to use the unordered_set class from the tr1 namespace to speed-up access against the plain (tree-based) STL map . However, I wanted to store references to threads ID in boost ( boost::thread::id ), and realized that the API of those identifiers is so opaque that you cannot clearly obtain a hash of it. Surprisingly, boost implements parts of the tr1 (including hash and unordered_set ), but it does not define a hash class that is able to hash a thread ID. Looking at the documentation of boost::thread::id I found that thread IDs can be output to a stream, so my solution for doing

set vs unordered_set for fastest iteration

旧城冷巷雨未停 提交于 2019-12-04 16:27:59
问题 In my application I have the following requirements - The data structure will be populated just once with some values (not key/value pairs). The values may be repeated but I want the data structure to store them once only. I will be iterating 100s of times through all the elements of the data structure created above. The order in which the elements appear in the iteration is immaterial. Constraint 1 suggests that I will either have to use set or unordered_set as data is not in the form of key