unordered-map

What should I pass to unordered_map's bucket count argument if I just want to specify a hash function?

大城市里の小女人 提交于 2019-12-01 02:11:07
C++11's unordered_map 's default constructor looks like this: explicit unordered_map( size_type bucket_count = /*implementation-defined*/, const hasher& hash = hasher(), const key_equal& equal = key_equal(), const allocator_type& alloc = allocator_type() ); I want to create an unordered_map with a custom hasher function, but it's the second argument to the constructor. What bucket count should I use? Is there a magic value I can use to tell the container to decide for itself? Otherwise, is there a heuristic I can use to guesstimate a good bucket number based on something like the number of

What should I pass to unordered_map's bucket count argument if I just want to specify a hash function?

耗尽温柔 提交于 2019-11-30 21:37:52
问题 C++11's unordered_map 's default constructor looks like this: explicit unordered_map( size_type bucket_count = /*implementation-defined*/, const hasher& hash = hasher(), const key_equal& equal = key_equal(), const allocator_type& alloc = allocator_type() ); I want to create an unordered_map with a custom hasher function, but it's the second argument to the constructor. What bucket count should I use? Is there a magic value I can use to tell the container to decide for itself? Otherwise, is

std::unordered_map pointers/reference invalidation

倖福魔咒の 提交于 2019-11-30 18:12:43
I have the following code: std::unordered_map<std::string, std::string> map; map["k1"] = "v1"; auto& v1 = map["k1"]; map["k2"] = "v2"; After reading http://en.cppreference.com/w/cpp/container/unordered_map Notes The swap functions do not invalidate any of the iterators inside the container, but they do invalidate the iterator marking the end of the swap region. References and pointers to either key or data stored in the container are only invalidated by erasing that element, even when the corresponding iterator is invalidated. It looks like v1 can be safely used after inserting new values,

How to measure Memory Usage of std::unordered_map

北城以北 提交于 2019-11-30 17:32:54
We know that hash table based container implementations like std::unordered_map use a lot memory but I don't know how much is how much? Apart from space complexity notations, and not considering if a container element is a pointer to a larger object: Is there any way to figure out how many bytes is being used by such a container during run-time? Is there a way to tell, at runtime, how much memory any container uses? If you want to get a rough size, I think bucket_count() and max_load_factor() is enough, which gives the current count of buckets and the load factor. Rational: If load_factor <= 1

Is an unordered_map really faster than a map in practice?

梦想的初衷 提交于 2019-11-30 09:11:51
Sure, the lookup performance of an unordered_map is constant on average, and the lookup performance of a map is O(logN). But of course in order to find an object in an unordered_map, we have to: hash the key we want to find. equality_compare the key with every key in the same bucket. Whereas in a map, we simply need to less_than compare the sought key with log2(N) keys, where N is the number of items in the map. I wondered what the real performance difference would be, given that the hash function adds overhead and an equality_compare is no cheaper than a less_than compare. Rather than bother

Defining custom hash function and equality function for unordered_map

醉酒当歌 提交于 2019-11-30 07:04:59
I am trying to define a type of unordered_map that has a custom hash function and equality comparison function. The function prototypes of these functions are as follows: //set<Vertex3DXT*> is the type of the key; Cell3DXT* is the type of the value size_t VertexSetHashFunction(set<Vertex3DXT*> vertexSet); //hash function bool SetEqual(set<Vertex3DXT*> a, set<Vertex3DXT*> b); //equality I have these function prototypes declared and then I try to declare the type as follows: typedef std::tr1::unordered_map<set<Vertex3DXT*>, Cell3DXT*, VertexSetHashFunction, SetEqual> CellDatabaseMapType; But it

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

非 Y 不嫁゛ 提交于 2019-11-30 05:15:36
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 (int i = 0; i < 10; i++) { c[i] = i + 1; b[i] = i * 2; } std::unordered_map<floatVector, int> map; map

std::unordered_map pointers/reference invalidation

你说的曾经没有我的故事 提交于 2019-11-30 02:11:12
问题 I have the following code: std::unordered_map<std::string, std::string> map; map["k1"] = "v1"; auto& v1 = map["k1"]; map["k2"] = "v2"; After reading http://en.cppreference.com/w/cpp/container/unordered_map Notes The swap functions do not invalidate any of the iterators inside the container, but they do invalidate the iterator marking the end of the swap region. References and pointers to either key or data stored in the container are only invalidated by erasing that element, even when the

OpenMP/__gnu_parallel for an unordered_map

独自空忆成欢 提交于 2019-11-29 15:14:55
At some point in my code I have to make operations on all elements in an unordered_map. In order to accelerate this process I want to use openMP, but the naive approach does not work: std::unordered_map<size_t, double> hastTable; #pragma omp for for(auto it = hastTable.begin(); it != hastTable.end(); it ++){ //do something } The reason for this is, that the iterator of an unordered_map is no random access iterator. As an alternative I have tried the __gnu_parallel directives working on for_each. But the following code #include <parallel/algorithm> #include <omp.h> __gnu_parallel::for_each

Using Boost unordered_map

荒凉一梦 提交于 2019-11-29 11:47:10
I want to include boost::unordered_map in my project without downloading the whole Boost package. How can I do that? Use bcp : http://www.boost.org/doc/libs/1_52_0/tools/bcp/doc/html/index.html cd $BOOST_DIR bcp unordered_map /tmp/TEST Now /tmp/TEST contains only the things required for unordered_map , in my case 15Mb (as opposed to 734Mb for the full boost library) You need at least headers because Boost packages depend on each other. You might want to select only needed header files but it will really be pain in the neck and will take you many hours. The algorithm is: Include only boost