unordered-map

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

ぐ巨炮叔叔 提交于 2019-11-27 06:48:08
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. The function object std::hash<> is used. Standard specializations exist for all built-in types, and some other standard library types such as std::string and std::thread . See the link for the full list. For other types to be used in a std:

c++ - unordered_map complexity

纵饮孤独 提交于 2019-11-27 06:40:26
问题 I need to create a lookup function where a (X,Y) pair corresponds to a specific Z value. One major requirement for this is that I need to do it in as close to O(1) complexity as I can. My plan is to use an unordered_map. I generally do not use a hash table for lookup, as the lookup time has never been important to me. Am I correct in thinking that as long as I built the unordered_map with no collisions, my lookup time will be O(1)? My concern then is what the complexity becomes if there the

a C++ hash map that preserves the order of insertion [duplicate]

只谈情不闲聊 提交于 2019-11-27 06:14:56
问题 This question already has an answer here: A std::map that keep track of the order of insertion? 14 answers I have the following code: #include <iostream> #include "boost/unordered_map.hpp" using namespace std; using namespace boost; int main() { typedef unordered_map<int, int> Map; typedef Map::const_iterator It; Map m; m[11] = 0; m[0] = 1; m[21] = 2; for (It it (m.begin()); it!=m.end(); ++it) cout << it->first << " " << it->second << endl; return 0; } However, I am looking for something that

C++ std::unordered_map complexity

我们两清 提交于 2019-11-27 06:03:40
问题 I've read a lot about unordered_map (c++11) time-complexity here at stackoverflow, but I haven't found the answer for my question. Let's assume indexing by integer (just for example): Insert/at functions work constantly (in average time), so this example would take O(1) std::unordered_map<int, int> mymap = { { 1, 1}, { 100, 2}, { 100000, 3 } }; What I am curious about is how long does it take to iterate through all (unsorted) values stored in map - e.g. for ( auto it = mymap.begin(); it !=

Using tuple in unordered_map

自古美人都是妖i 提交于 2019-11-27 02:46:21
问题 I want to use tuple consisting of int , char , char in my unordered_map . I am doing like this: #include <string> #include <unordered_map> #include <cstring> #include <iostream> #include <tuple> using namespace std; tuple <int,char,char> kk; unordered_map<kk,int> map; int main() { map[1,"c","b"]=23; return 0; } but this gives me following errors: map.cpp:9:21: error: type/value mismatch at argument 1 in template parameter list for ‘template<class _Key, class _Tp, class _Hash, class _Pred,

When do you use std::unordered_map::emplace_hint?

落花浮王杯 提交于 2019-11-27 02:38:16
问题 I know how to use std::unordered_map::emplace , but how do I use emplace_hint ? Neither cplusplus nor cppreference provide a set of examples that illustrate how we might know where to put the element. Can anyone provide some information on this or give some examples / illustrations of when we might know where the emplaced element should go? 回答1: What could an unordered_map potentially do with the hint? Well, if the iterator addresses an element with the same key as the element that emplace

C++ unordered_map fail when used with a vector as key

荒凉一梦 提交于 2019-11-27 01:39:19
问题 Background: I am comming from the Java world and I am fairly new to C++ or Qt. In order to play with unordered_map, I have written the following simple program: #include <QtCore/QCoreApplication> #include <QtCore> #include <iostream> #include <stdio.h> #include <string> #include <unordered_map> using std::string; using std::cout; using std::endl; typedef std::vector<float> floatVector; int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); floatVector c(10); floatVector b(10); for

Is the unordered_map really unordered?

杀马特。学长 韩版系。学妹 提交于 2019-11-26 21:15:53
问题 I am very confused by the name 'unordered_map'. The name suggests that the keys are not ordered at all. But I always thought they are ordered by their hash value. Or is that wrong (because the name implies that they are not ordered)? Or to put it different: Is this typedef map<K, V, HashComp<K> > HashMap; with template<typename T> struct HashComp { bool operator<(const T& v1, const T& v2) const { return hash<T>()(v1) < hash<T>()(v2); } }; the same as typedef unordered_map<K, V> HashMap; ? (OK

Choosing between std::map and std::unordered_map [duplicate]

落花浮王杯 提交于 2019-11-26 19:26:16
This question already has an answer here: Is there any advantage of using map over unordered_map in case of trivial keys? 12 answers Now that std has a real hash map in unordered_map , why (or when) would I still want to use the good old map over unordered_map on systems where it actually exists? Are there any obvious situations that I cannot immediately see? Arun As already mentioned , map allows to iterate over the elements in a sorted way, but unordered_map does not. This is very important in many situations, for example displaying a collection (e.g. address book). This also manifests in

unordered_map hash function c++

佐手、 提交于 2019-11-26 18:45:23
I need to define an unordered_map like this unordered_map<pair<int, int>, *Foo> , what is the syntax for defining and passing a hash and equal functions to this map? I've tried passing to it this object: class pairHash{ public: long operator()(const pair<int, int> &k) const{ return k.first * 100 + k.second; } }; and no luck: unordered_map<pair<int, int>, int> map = unordered_map<pair<int, int>, int>(1, *(new pairHash())); I have no Idea what is the size_type_Buskets means so I gave it 1 . What is the right way to do it? Thanks. This is an unfortunate omission in C++11; Boost has the answer in