unordered-map

What is the default hash function used in C++ std::unordered_map?

懵懂的女人 提交于 2019-12-17 08:47:16
问题 I am using unordered_map<string, int> and unordered_map<int, int> What hash function is used in each case and what is chance of collision in each case? I will be inserting unique string and unique int as keys in each case respectively. I am interested in knowing the algorithm of hash function in case of string and int keys and their collision stats. 回答1: The function object std::hash<> is used. Standard specializations exist for all built-in types, and some other standard library types such

What is the default hash function used in C++ std::unordered_map?

馋奶兔 提交于 2019-12-17 08:47:11
问题 I am using unordered_map<string, int> and unordered_map<int, int> What hash function is used in each case and what is chance of collision in each case? I will be inserting unique string and unique int as keys in each case respectively. I am interested in knowing the algorithm of hash function in case of string and int keys and their collision stats. 回答1: The function object std::hash<> is used. Standard specializations exist for all built-in types, and some other standard library types such

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

C++ unordered_map with char* key produces unexpected behavior

試著忘記壹切 提交于 2019-12-13 06:12:54
问题 I attempted to use an unordered_map to hash a char* key to an integer value. After writing custom functors to hash and compare char*, the unordered map appeared to work. However, I eventually noticed that the hash would occasionally return incorrect results. I created a test project to reproduce the error. The code below creates an unordered_map with a char* key and custom functors. It then runs 1000x cycles and records any hash errors that occurred. I am wondering if there is something wrong

Visual C++ 2010 : unordered_map and move constructor

纵然是瞬间 提交于 2019-12-13 05:56:52
问题 I have a structure named Foo which contains a unique_ptr struct Foo { std::unique_ptr<Bar> pointer; }; Now I'm trying to store instances of Foo in an unordered_map std::unordered_map<int,Foo> myMap; Technically this should be possible, since maps do not require a copy constructor, only a move constructor. However, I can't insert an element in my map: myMap.insert(std::make_pair(3, Foo())); This line will generate the following error in Visual C++ 2010 (roughly translated by me, since my

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:

C++ sorting algorithm

情到浓时终转凉″ 提交于 2019-12-12 18:57:26
问题 Thanks for looking at this question in advance. I am trying to order the following list of items: Bpgvjdfj,Bvfbyfzc Zjmvxouu,Fsmotsaa Xocbwmnd,Fcdlnmhb Fsmotsaa,Zexyegma Bvfbyfzc,Qkignteu Uysmwjdb,Wzujllbk Fwhbryyz,Byoifnrp Klqljfrk,Bpgvjdfj Qkignteu,Wgqtalnh Wgqtalnh,Coyuhnbx Sgtgyldw,Fwhbryyz Coyuhnbx,Zjmvxouu Zvjxfwkx,Sgtgyldw Czeagvnj,Uysmwjdb Oljgjisa,Dffkuztu Zexyegma,Zvjxfwkx Fcdlnmhb,Klqljfrk Wzujllbk,Oljgjisa Byoifnrp,Czeagvnj Into the following order: Bpgvjdfj Bvfbyfzc Qkignteu

g++ unordered_map has no at() function?

*爱你&永不变心* 提交于 2019-12-12 16:13:57
问题 I've been developing using Visual Studio 2010, and then compiling a Linux 64 version on another machine. To cover the difference between the 2 different compilers/environments, we have conditional include statements: #ifdef __linux__ #include <tr1/unordered_map> #endif #ifdef _WIN32 #include <unordered_map> #endif using namespace std; // covers std::unordered_map using namespace std::tr1; // covers tr/unordered_map unordered_map<string,string> map; For unordered_map , I've been using this

Difference between std::unordered_map < K, boost::ptr_deque < T > >'s operator[] (K const &) and emplace

ぐ巨炮叔叔 提交于 2019-12-12 04:58:48
问题 #include <memory> #include <unordered_map> #include <vector> #include <utility> #include <boost/ptr_container/ptr_deque.hpp> struct T { T() = default; T(T const &) = delete; T & operator = (T const &) = delete; T(T &&) = default; T & operator = (T &&) = default; }; using S = boost::ptr_deque < T >; int main() { std::unordered_map < uint32_t, S > testum; // testum.emplace(1u, S()); // testum.insert(std::make_pair(1u, S())); testum[1].push_back(new T()); } In the above example, the commented