unordered-map

Why STL unordered_map and unordered_set cannot be sorted by STL algorithms?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-01 17:37:47
问题 I'll start by illustrating a simple use case example: Consider the problem of a social security ID database, where in C++ code is modelled as a std::unordered_map where its key is the social security ID of a person and its value is a std::string with the full-name of that person (e.g., std::unordered_map<int, std::string> DB; ). Consider also, that there's a request for printing this database sorted in ascending order based on the person's ID (i.e., std::unordered_map 's key). Naively, one

C++ unordered_map<string, …> lookup without constructing string

余生长醉 提交于 2019-12-01 17:29:21
问题 I have C++ code that investigates a BIG string and matches lots of substrings. As much as possible, I avoid constructing std::strings, by encoding substrings like this: char* buffer, size_t bufferSize At some point, however, I'd like to look up a substring in one of these: std::unordered_map<std::string, Info> stringToInfo = {... So, to do that, I go: stringToInfo.find(std::string(buffer, bufferSize)) That constructs a std::string for the sole purpose of the lookup. I feel like there's an

unordered_map::find with key std::pair of pointers with custom hash crashes in VS2012

江枫思渺然 提交于 2019-12-01 13:34:30
I needed a std::unordered_map with key a std::pair<T*, T*> so I "stole" the following code: template <class T> inline void hash_combine(std::size_t & seed, const T & v) { std::hash<T> hasher; seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2); } namespace std { template<typename S, typename T> struct hash<pair<S, T>> { inline size_t operator()(const pair<S, T> & v) const { size_t seed = 0; ::hash_combine(seed, v.first); ::hash_combine(seed, v.second); return seed; } }; } from this stackoverflow answer . It works like a charm on linux machines with gcc 4.9.2. However in windows visual

std::unordered_map - how to “track” max/min key at any time

有些话、适合烂在心里 提交于 2019-12-01 12:58:40
I have std::unordered_map<int, int> . I do not want to use other structures like tree or anything else cause latency requirements. But at any time I need to know current max key and min key. How can I do that? Distribution is NOT uniform, instead max and min are frequently removed and inserted. So I need something smarter than "just scan entire map for a new max/min when current max/min is removed". I do not want to use any other structures. I want to use std::unordered_map ! upd according to the answer created such structure: struct OrderBookItem { int64_t price; int32_t lots; }; typedef

std::unordered_map - how to “track” max/min key at any time

无人久伴 提交于 2019-12-01 12:02:12
问题 I have std::unordered_map<int, int> . I do not want to use other structures like tree or anything else cause latency requirements. But at any time I need to know current max key and min key. How can I do that? Distribution is NOT uniform, instead max and min are frequently removed and inserted. So I need something smarter than "just scan entire map for a new max/min when current max/min is removed". I do not want to use any other structures. I want to use std::unordered_map ! upd according to

unordered_map::find with key std::pair of pointers with custom hash crashes in VS2012

拜拜、爱过 提交于 2019-12-01 10:51:15
问题 I needed a std::unordered_map with key a std::pair<T*, T*> so I "stole" the following code: template <class T> inline void hash_combine(std::size_t & seed, const T & v) { std::hash<T> hasher; seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2); } namespace std { template<typename S, typename T> struct hash<pair<S, T>> { inline size_t operator()(const pair<S, T> & v) const { size_t seed = 0; ::hash_combine(seed, v.first); ::hash_combine(seed, v.second); return seed; } }; } from this

Iterator invalidation in boost::unordered_map

会有一股神秘感。 提交于 2019-12-01 06:11:53
I am using boost::unordered_map as follows typedef boost::shared_ptr<WriterExeciter> PtrWriter; typedef std::list<PtrWriter> PtrList; boost::unordered_map<std::pair<unsigned int, unsigned long long>, PtrList> Map Map instrMap; Now I am making some changes to the list of type PtrList in a loop for(auto it = instrMap.begin(); it != instrMap.end(); ++it) { auto key = it->first(); auto list& = it->second(); //Make some change to an element in list if(list.empty()) { instMap.erase(key); } } Does making changes to the list invalidate the iterator to instrMap? Erasing the element will invalidate the

Why is std::unordered_map slow, and can I use it more effectively to alleviate that?

懵懂的女人 提交于 2019-12-01 05:29:25
I’ve recently found out an odd thing. It seems that calculating Collatz sequence lengths with no caching at all is over 2 times faster than using std::unordered_map to cache all elements . Note I did take hints from question Is gcc std::unordered_map implementation slow? If so - why? and I tried to used that knowledge to make std::unordered_map perform as well as I could (I used g++ 4.6, it did perform better than recent versions of g++, and I tried to specify a sound initial bucket count, I made it exactly equal to the maximum number of elements the map must hold). In comparision, using std:

Iterator invalidation in boost::unordered_map

浪子不回头ぞ 提交于 2019-12-01 04:56:00
问题 I am using boost::unordered_map as follows typedef boost::shared_ptr<WriterExeciter> PtrWriter; typedef std::list<PtrWriter> PtrList; boost::unordered_map<std::pair<unsigned int, unsigned long long>, PtrList> Map Map instrMap; Now I am making some changes to the list of type PtrList in a loop for(auto it = instrMap.begin(); it != instrMap.end(); ++it) { auto key = it->first(); auto list& = it->second(); //Make some change to an element in list if(list.empty()) { instMap.erase(key); } } Does

C++ some questions on boost::unordered_map & boost::hash

荒凉一梦 提交于 2019-12-01 04:07:56
I've only recently started dwelling into boost and it's containers, and I read a few articles on the web and on stackoverflow that a boost::unordered_map is the fastest performing container for big collections. So, I have this class State, which must be unique in the container (no duplicates) and there will be millions if not billions of states in the container. Therefore I have been trying to optimize it for small size and as few computations as possible. I was using a boost::ptr_vector before, but as I read on stackoverflow a vector is only good as long as there are not that many objects in