unordered-map

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

独自空忆成欢 提交于 2019-12-12 04:17:02
问题 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;

What's a good hash function for struct with 3 unsigned chars and an int, for unordered_map?

可紊 提交于 2019-12-11 18:09:56
问题 I just want to use a unordered_map with my struct as key, since I dont need any ordering..but I just cant find myself with all that hash stuff.. As a side relevant question..When ppl compare unordered and ordered map they never talk about the hash function, how can that be? Cant a bad hash function makes unordered map slower than map? (solely due the hash function) struct exemple{ unsigned char a,b,c; unsigned int n; bool operator == ( const exemple & other) const {..} }; namespace std {

c++ unorderedmap vector subscript out of range

ⅰ亾dé卋堺 提交于 2019-12-11 15:07:24
问题 Currently when I have been calling my getFunction method I am getting nasty crash. My program compiles fine, but when I run and call this function, I get "Debug Assertion Failed!", "Expression: vector subscript out of range". Not sure how to deal with this as I haven't been doing much in c++ for a few years. void* PluginMap::getFunction(char* pguid, char* fname){ if(plugin_map.size()>0 && plugin_map.find(pguid)!=plugin_map.end()) { //plugin_map is an unorderedmap that is defined elsewhere.

std::unordered_map containing another std::unordered_map?

烈酒焚心 提交于 2019-12-11 14:52:27
问题 We are working on a game project for school in c++. I'm in charge of the map object, who will contain Entities like bombs, players, walls and boxes. I have 3 containers in my map: A std::list for players (multiple players can stand on a same case). A std::unordered_map<int, std::unordered_map<int, AEntity*> > for walls. An other std::unordered_map<int, std::unordered_map<int, AEntity*> > for bombs+boxes. The aim is to access an Entity very fast on the map, number of entities can be very high.

`std::unordered_map` emplace error without `std::piecewise_construct`

有些话、适合烂在心里 提交于 2019-12-11 09:54:38
问题 I have the following code: #include <complex> #include <tuple> #include <unordered_map> using namespace std; int main() { unordered_map<string, complex<double>> um; um.emplace(piecewise_construct, make_tuple("test1"), make_tuple(1,1));// works // um.emplace("test2", {1,1}); // error here complex<double> z{1,1}; um.emplace("test3", z); // this works } I do not completely understand why am I getting an error on the commented line. The unordered_map is emplacing a pair<string, complex<double>> ,

Using unordered_map with custom value object in C++ [closed]

喜夏-厌秋 提交于 2019-12-11 09:29:36
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 years ago . I'm trying to create an unordered_map where the key is of type char and the value is a pointer to my own custom class. Is this possible? Whenever I try compiling I get this among other errors: $ g++ -std=c++0x -o spellchecker spellchecker.cpp -lstdc++ spellchecker.cpp:17:27: error: template argument 2 is invalid

avoid temporary std::string to invoke boost::unordered_map::find

ε祈祈猫儿з 提交于 2019-12-11 07:32:11
问题 I have the following type: boost::unordered_map< std::string , Domain::SomeObject > objectContainer; which is just a map to some domain object, using std::strings as keys. Now, std::string can be constructed and compared with const char* . (no need for an explicit std::string temporary, although maybe a implicit conversion is happening?) The problem happens when I try to do something like void findStuff(const char* key) { auto it = objectContainer.find(key); //<---build error } My main

How to use BOOST_FOREACH with an Unordered_map?

家住魔仙堡 提交于 2019-12-11 04:36:53
问题 OK, so here's my situation - pretty straightforward but I'm not sure how it can work (I can find no documentation whatsoever...) : I have an Unordered_map : typedef unsigned long long U64; typedef boost::unordered_map<U64, U64> HASH; And I would like to loop through the elements (mainly the keys), pretty much like using PHP foreach , but this time using BOOST_FOREACH , I suspect something like : HASH myMap; // .. assignment, etc... BOOST_FOREACH (U64 key, myMap) { // do sth with the Key-Value

unorder_map<float, short> Why does this work?

三世轮回 提交于 2019-12-11 04:28:12
问题 I am using an unordered_map>float, unsigned short> to implement a hash table in C++. I know that using floats as keys to a hash table is a BAD idea under most circumstances because comparing them is error prone. However, under these circumstances I am reading the floats in from large files and their precision is known and constant. However, I would like to know the details of how unordered_map is hashing my floats in order to estimate collision frequency. I am not overriding the default hash

How to store more than 2 variables in an unordered_map?

大憨熊 提交于 2019-12-11 03:22:35
问题 How can I store more than 2 variables in an std::unordered_map ? I want something like this: std::unordered_map<string, int, int, int> mapss = {{"a",1,1,1},{"b",1,2,3}}; 回答1: If the string is the key, and the rest are values, then you could have the value be a tuple . unordered_map<string, tuple<int, int, int>> mapss Of if you don't know how many values will be there, you can use a vector unordered_map<string, vector<int>> mapss 回答2: You can use an std::tuple , as Cyber mentioned, but I