unordered-map

std::unordered_map::emplace issue with private/deleted copy constructor

浪尽此生 提交于 2020-01-03 08:09:40
问题 The following code compiles fine with gcc 4.7.2 (mingw) #include <unordered_map> #include <tuple> struct test { test() =default; private: test(test const&) =delete; }; int main() { std::unordered_map<char, test> map; map.emplace( std::piecewise_construct, std::forward_as_tuple('a'), std::forward_as_tuple() ); } If I change the copy constructor in test from test(test const&) =delete; to test(test const&) =default; however, the template error vomit seems to complain about const test& not being

C++ unordered_map using a custom class type as the key

ぃ、小莉子 提交于 2020-01-03 05:08:16
问题 I am trying to use a custom class as key for an unordered_map , like the following: #include <iostream> #include <algorithm> #include <unordered_map> using namespace std; class node; class Solution; class Node { public: int a; int b; int c; Node(){} Node(vector<int> v) { sort(v.begin(), v.end()); a = v[0]; b = v[1]; c = v[2]; } bool operator==(Node i) { if ( i.a==this->a && i.b==this->b &&i.c==this->c ) { return true; } else { return false; } } }; int main() { unordered_map<Node, int> m;

how to use boost::unordered_map

这一生的挚爱 提交于 2020-01-03 02:26:12
问题 for my application, i need to use a hash map, so i have written a test program in which i store some instances of a baseclass in a boost::unordered_map. but i want to reach the instances by calling special functions which return a derived class of the base and i use those functions' parameters for hash key of unordered_map. if no class is found with certain parameters then a class is generated and stored in map. the purpose of the program may not be clear but here is the code. #include <boost

Reversing or reverse iterating an unordered_map

与世无争的帅哥 提交于 2020-01-02 02:12:50
问题 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. 回答1: Just reading the first sentence in your question gives you the answer: I'm trying to print the contents of an

Faster way to read/write a std::unordered_map from/to a file

点点圈 提交于 2020-01-01 09:30:11
问题 I am working with some very large std::unordered_map s (hundreds of millions of entries) and need to save and load them to and from a file. The way I am currently doing this is by iterating through the map and reading/writing each key and value pair one at a time: std::unordered_map<unsigned long long int, char> map; void save(){ std::unordered_map<unsigned long long int, char>::iterator iter; FILE *f = fopen("map", "wb"); for(iter=map.begin(); iter!=map.end(); iter++){ fwrite(&(iter->first),

Hashing pointers as Keys for unordered_map in C++ STL

一曲冷凌霜 提交于 2020-01-01 09:23:40
问题 I posted a similar quetion regarding using pointers as Keys on maps in C++ STL. How are pointers hashed in unordered_maps when used as Keys. More specifically if I define: std::unordered_map< CustomClass*, int > foo; Would the default C++ std::hash implementation work to handle these pointers? Is it safe to use? Is this good practice? 回答1: std::hash<T*> is defined but the details of how it operates are implementation dependent. It will certainly be safe to use, and I'd consider it good

C++ stl unordered_map implementation, reference validity

南笙酒味 提交于 2019-12-28 20:33:30
问题 For both std::map and std::tr1::unordered_map , I see from the standard that: References to elements in the unordered_map container remain valid in all cases, even after a rehash. How are they doing that ( implementation-wise )? Are they maintaining all the entries as a kind of linked list and then the hash-table just stores pointers to the elements? 回答1: Yes, linked lists are involved, although not quite in the way you suggest. The 2011 standard says (23.2.5 para 8), "The elements of an

Trouble creating custom hash function unordered_map?

≡放荡痞女 提交于 2019-12-25 12:43:52
问题 I wanted to create a custom hash function for an unordered map. I found this question: C++ unordered_map fail when used with a vector as key and found that if you use a vector as a key in an unordered map, you need to create your own hash function. I experimented copying the hash function written as so: template <typename Container> struct container_hash { std::size_t operator()(Container const& c) const { return boost::hash_range(c.begin(), c.end()); } }; But when I try to create an

Trouble creating custom hash function unordered_map?

对着背影说爱祢 提交于 2019-12-25 12:43:10
问题 I wanted to create a custom hash function for an unordered map. I found this question: C++ unordered_map fail when used with a vector as key and found that if you use a vector as a key in an unordered map, you need to create your own hash function. I experimented copying the hash function written as so: template <typename Container> struct container_hash { std::size_t operator()(Container const& c) const { return boost::hash_range(c.begin(), c.end()); } }; But when I try to create an

This hash only works for enumeration types

会有一股神秘感。 提交于 2019-12-24 14:25:28
问题 I'm working on a (very) simple class in C++ that has a unordered_map member: class SDLFontManager : public CPObject, public FontManagerProtocol { public: SDLFontManager() {}; // flush the cache when the manager is destroyed virtual ~SDLFontManager() { flushFontCache(); }; // load a font from the cache if it exists, or load it from file and cache it. virtual FontProtocol* fontWithTTF(const char* filename) { if(_cache.find(filename) != _cache.end()) { return _cache[filename]; } SDLFont* font =