unordered-map

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

hybrid linked list constructed on unordered_map?

有些话、适合烂在心里 提交于 2019-12-24 03:16:28
问题 Hi I wonder if I can set up another linked struct myself to actually set up my own order between keys in the unordered_map? or there is a standard library? I need the fast look up function of unordered_map... For example: #include<string> #include<tr1/unordered_map> struct linker { string *pt; string *child1; string *child2; }; unordered_map<string,int> map({{"aaa",1},{"bbb",2},{"ccc",3},{"ddd",4}}); linker node1 = new linker; node1.pt = &map.find("aaa")->first; node1.child1 = &map.find("ccc"

Having a composite key for hash map in c++

好久不见. 提交于 2019-12-22 09:44:08
问题 I have a data structure which has, <Book title>, <Author>, and <rate> Since Book title or Author can be duplicated, I'd like to build a composite key. (let's say I cannot make extra unique key, such as ID) Since data is pretty huge, I'm using GCC unordered_map for the sake of speed, and I built my structure like this: typedef pair<string, string> keys_t typedef unordered_map<keys_t, double> map_t; Everything works okay in general, But the problem happens when I want to refer one specific key.

How to check for TR1 while compiling?

点点圈 提交于 2019-12-22 06:49:34
问题 We are programming a logging library that keeps itself in a .hpp file. We would like to include <tr1/unordered_map> (if the compiler supports TR1,) or the standard <map> otherwise. Is there a standard way of checking at compile time if tr1 is available or not? I was thinking that the same way that the " __cplusplus " define symbol is present, there could have been defined a " __cxx__tr1 " or something like that. I haven't seen that in the drafts for TR1, so I assume it is not present, but I

Python dictionaries vs C++ std:unordered_map (cython) vs cythonized python dict

偶尔善良 提交于 2019-12-22 06:47:12
问题 I was trying to measure the performance between python dictionaries, cythonized python dictionaries and cythonized cpp std::unordered_map doing only a init procedure. If the cythonized cpp code is compiled I thought it should be faster than the pure python version. I did a test using 4 different scenario/notation options: Cython CPP code using std::unordered_map and Cython book notation (defining a pair and using insert method) Cython CPP code using std::unordered_map and python notation (map

Why is {} used to access operator() in std::hash?

安稳与你 提交于 2019-12-22 06:46:51
问题 While reading the examples of std::hash used for std::unordered_map, I noticed that the operator() function was being accessed by {}. http://en.cppreference.com/w/cpp/utility/hash result_type operator()(argument_type const& s) const { result_type const h1 ( std::hash<std::string>{}(s.first_name) ); result_type const h2 ( std::hash<std::string>{}(s.last_name) ); return h1 ^ (h2 << 1); // or use boost::hash_combine (see Discussion) } What does the use of {} here represent? 回答1: std::hash<T> is

Is libstdc++ support for std::unordered_map incomplete?

旧街凉风 提交于 2019-12-22 04:39:18
问题 Related to this question on CodeReview, I tried to use std::unordered_map with a custom allocator but apparently this does not work with gcc/clang and libstdc++. The error can be generated from initializing an empty hash map with a std::allocator #include <unordered_map> int main() { typedef std::allocator<std::pair<const int, int>> A; typedef std::unordered_map<int, int, std::hash<int>, std::equal_to<int>, A> H; auto h = H{A()}; // ERROR, cannot find constructor H::H(const A&) } Live Example

c++ unordered_map collision handling , resize and rehash

吃可爱长大的小学妹 提交于 2019-12-21 07:57:40
问题 I have not read the C++ standard but this is how I feel that the unordered_map of c++ suppose to work. Allocate a memory block in the heap. With every put request, hash the object and map it to a space in this memory During this process handle collision handling via chaining or open addressing.. I am quite surprised that I could not find much about how the memory is handled by unordered_map. Is there a specific initial size of memory which unordered_map allocates. What happens if lets say we

C++11: Is it safe to remove individual elements from std::unordered_map while iterating?

眉间皱痕 提交于 2019-12-21 06:57:35
问题 Consider the canonical algorithm for removing an element from an associative container while iterating: for (auto iter = myMap.begin(); iter != myMap.end(); ) { if (/* removal condition */) { iter = myMap.erase(iter); } else { ++iter; } } I've been applying this algorithm without a second thought when using the C++11 std::unordered_map container. However, after browsing the documentation for std::unordered_map::erase on cppreference.com, I became a little concerned after reading the following

Obtaining list of keys and values from unordered_map

旧城冷巷雨未停 提交于 2019-12-20 08:55:58
问题 What is the most efficient way of obtaining lists (as a vector ) of the keys and values from an unordered_map ? For concreteness, suppose the map in question is a unordered_map<string, double> . I'd then like to obtain the keys as a vector<string> , and the values as a vector<double> . unordered_map<string, double> um; vector<string> vs = um.enum_keys(); vector<double> vd = um.enum_values(); I can just iterate across the map and collect the result, but is there a more efficient method? It