unordered-set

Fastest Way to Determine if Character Belongs to a Set of Known Characters C++

感情迁移 提交于 2020-01-03 11:33:30
问题 Given any character, what's the fastest way for me to determine if that character belongs to a set (not the container-type) of known characters. In other words, what's the fastest elegant way to implement the conditional: char c = 'a'; if(c == ch1 || c == ch2 || c == ch3 ...) // Do something... Is there an STL container (I'm thinking it might be unordered_set?) that I can just pass the character as a key to and it'll return true if the key exists? Anything with an O(1) lookup time will work

Fastest Way to Determine if Character Belongs to a Set of Known Characters C++

☆樱花仙子☆ 提交于 2020-01-03 11:33:09
问题 Given any character, what's the fastest way for me to determine if that character belongs to a set (not the container-type) of known characters. In other words, what's the fastest elegant way to implement the conditional: char c = 'a'; if(c == ch1 || c == ch2 || c == ch3 ...) // Do something... Is there an STL container (I'm thinking it might be unordered_set?) that I can just pass the character as a key to and it'll return true if the key exists? Anything with an O(1) lookup time will work

How to make a c++11 std::unordered_set of std::weak_ptr

自作多情 提交于 2019-12-30 08:13:17
问题 I have a set like this: set<weak_ptr<Node>, owner_less<weak_ptr<Node> > > setName; It works fine. But I would like to change it to an unordered set. However, I get about six pages of errors when I do that. Any ideas how to do that? After looking through all the pages of error messages I found to lines that might help. /usr/include/c++/4.7/bits/functional_hash.h:60:7: error: static assertion failed: std::hash is not specialized for this type /usr/include/c++/4.7/bits/stl_function.h: In

Unordered_set questions

*爱你&永不变心* 提交于 2019-12-25 02:04:32
问题 Could anyone explain how an unordered set works? I am also not sure how a set works. My main question is what is the efficiency of its find function. For example, what is the total big O run time of this? vector<int> theFirst; vector<int> theSecond; vector<int> theMatch; theFirst.push_back( -2147483648 ); theFirst.push_back(2); theFirst.push_back(44); theSecond.push_back(2); theSecond.push_back( -2147483648 ); theSecond.push_back( 33 ); //1) Place the contents into a unordered set that is O(m

Are there faster hash functions for unordered_map/set in C++?

六月ゝ 毕业季﹏ 提交于 2019-12-24 05:48:44
问题 Default function is from std::hash. I wonder if there are better hash functions for saving computational time? for integer keys as well as string keys. I tried City Hash from Google for both integer and string keys, but its performance is a little worse than std::hash. 回答1: std::hash functions are already good in performance. I think you should try open source hash functions. Check this out https://github.com/Cyan4973/xxHash. I quote from its description: "xxHash is an Extremely fast Hash

Is std::unordered_set contiguous (like std::vector)?

半城伤御伤魂 提交于 2019-12-23 09:38:39
问题 I'm storing pointers in std::unordered_set, because I don't want any duplicate (I delete the pointers in the collection, so if there is a duplicate I will attempt to delete an already deleted pointer). I loop heavily through these sets, and since I know std::vector is the fastest container for looping (contiguous memory), I wondered if std::unordered_set does the same. If it doesn't, would using a std::vector and checking if the pointer has been deleted be faster? 回答1: Is std::unordered_set

Random element from unordered_set in O(1)

浪子不回头ぞ 提交于 2019-12-18 05:43:51
问题 I've seen people mention that a random element can be grabbed from an unordered_set in O(1) time. I attempted to do so with this: std::unordered_set<TestObject*> test_set; //fill with data size_t index = rand() % test_set.size(); const TestObject* test = *(test_set.begin() + index); However, unordered_set iterators don't support + with an integer. begin can be given a size_t param, but it is the index of a bucket rather than an element. Randomly picking a bucket then randomly picking an

How to specialize std::hash<T> for user defined types?

你离开我真会死。 提交于 2019-12-14 03:44:48
问题 The Question What is a good specialization of std::hash for use in the third template parameter of std::unordered_map or std::unordered_set for a user defined type for which all member data types already have a good specialization of std::hash? For this question, I define "good" as simple to implement and understand, reasonably efficient, and unlikely to produce hash table collisions. The definition of good does not include any statements about security. The State of What is Google'able At

Unordered-MultiMap of Pairs

ぐ巨炮叔叔 提交于 2019-12-13 04:51:21
问题 I'm struggling to efficiently store information in pairs: For instance I have two structs that represent (x,y) coords that i wish to calculate and store the distance between. Currently I am store all the values twice in a unordered_map<pair<Struct1*,Struct2*,double> My problem is that when searching I want the result <Struct1*,Struct2*> to turn up the same value as <Struct2*,Struct1*> that way I do not have to store information twice. I've thought about using a multimap but I think that std:

Why is std::unordered_set rehashed even if the load factor limit is not broken?

天涯浪子 提交于 2019-12-12 14:27:25
问题 According to cppreference, Rehashing occurs only if the new number of elements is greater than max_load_factor()*bucket_count() . In addition, [unord.req]/15 has similar rules: The insert and emplace members shall not affect the validity of iterators if (N+n) <= z * B , where N is the number of elements in the container prior to the insert operation, n is the number of elements inserted, B is the container's bucket count, and z is the container's maximum load factor. However, consider the