unordered-map

How to check for TR1 while compiling?

牧云@^-^@ 提交于 2019-12-05 11:05:31
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 wanted to ask first just in case. As a note, if those defines don't exist, it wouldn't be a bad idea to

Why does C++11/Boost `unordered_map` not rehash when erasing?

心已入冬 提交于 2019-12-05 09:27:50
I'm wondering why both C++11 and Boost's hashmap does not resize while erasing elements through iteration. Even if that is not technically a memory leak I think it could be a serious issue in applications (it was a hidden issue for me, had hard time to track it back) and it could actually affecting many applications. Is this a "design flaw" with the container? I benchmarked it and seems to be affecting several compiler releases (including VS, Clang, GCC) The code to reproduce the issue is: std::unordered_map<T1,T2> m; for (int i = 0; i < 5000000; i++) m.insert(std::make_pair(i, new data_type))

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

佐手、 提交于 2019-12-05 09:09:35
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? std::hash<T> is a type not a function. An instance of std::hash has an operator() that does the hash. So std::hash<std:

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

岁酱吖の 提交于 2019-12-05 08:41:14
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[key] = value) Cython code (typed code) using python dictionaries (map[key] = value) Pure python code I

unordered_map with reference as value

我们两清 提交于 2019-12-05 07:46:34
Is it legal to have an unordered_map with the value type being a reference C++11? For example std::unordered_map<std::string, MyClass&> I have managed to get this to compile with VS2013 however I'm not sure whether it's supposed to as it is causing some strange runtime errors. For example vector subscript out of range is thrown when trying to erase an element. Some googling resulted in finding out that you can't have a vector of references but I can't find anything about an unordered_map. Update Further experimentation has shown that the vector subscript out of range was not related to the

C++ How to insert array to unordered_map as its key?

瘦欲@ 提交于 2019-12-05 07:38:12
Hi I used to have a unordered_set to hold my 16 int array, now I need to store one more int as its bucket. I wonder if I can insert the array into my unordered_set, or can I use the same template I used to use? #include <unordered_set> #include <array> namespace std { template<typename T, size_t N> struct hash<array<T, N> > { typedef array<T, N> argument_type; typedef size_t result_type; result_type operator()(const argument_type& a) const { hash<T> hasher; result_type h = 0; for (result_type i = 0; i < N; ++i) { h = h * 31 + hasher(a[i]); } return h; } }; } std::unordered_set<std::array<int,

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

非 Y 不嫁゛ 提交于 2019-12-05 05:34:43
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 . Question : is libstdc++ support for constructing std::unordered_map with a single allocator as

Does C++11 provide hashing functions for std::type_info?

一曲冷凌霜 提交于 2019-12-05 04:11:34
I'm still working on a good solution to my One-Of-A-Type Container Problem -- and upon reflection I think it would be nice to be able to just use something like a std::map<std::type_info, boost::any> . Unfortunately, std::type_info does not define an operator< , and I think it'd be unreasonable for it to define one. However, it does seem reasonable to define a hash function for it, because you could simply use the singleton address of the std::type_info object as a reasonable "hash". Therefore, you'd be able to put a std::type_info into a std::unordered_map as the key. Does C++11 provide such

Reversing or reverse iterating an unordered_map

旧街凉风 提交于 2019-12-05 03:40:49
I'm trying to print the contents of an unordered_map in reverse order, but it doesn't have rbegin or rend, so you can't use reverse_iterator. How is reversing of the unordered_map supposed to be done? EDIT (from comment): I want the keys to be in the order they were inserted in, hence I cant use map. The keys appear to stay in the insertion order, but I need them reversed. Just reading the first sentence in your question gives you the answer: I'm trying to print the contents of an unordered_map in reverse order You cannot print in any order because, well, it is unordered. It makes no sense

Basic questions: Pointers to objects in unordered_maps (C++)

我是研究僧i 提交于 2019-12-05 02:03:30
问题 I'm new to C++ programming and would greatly appreciate replies that don't assume much prior knowledge. Thanks to suggestions here, I've created an unordered map: typedef std::tr1::unordered_map<std::string, Strain*> hmap; The data in this map are pointers to instances of class Strain. As soon as these instances are created, I create pointers to them, and I then add these pointers to my hash table (hmap strainTable) and to another vector (vector< Strain *> liveStrains), e.g., string MRCA; for