FAQ Database Discussion Community


incrementing iterator of std::unordered_map

c++,stl,unordered-map
Why are lines 7 and 8 bad?? Why it's bad to in-/decrement some iterator? #include <unordered_map> int main() { std::unordered_multimap<int,int> myumm({{1,3},{3,2},{5,5},{0,9}}); auto first = myumm.begin(); auto second = first+1; // bad auto third = --myumm.end(); // bad too auto fourth = myumm.end(); } ...

Adding into unordered map containing vector value

c++,dictionary,vector,unordered-map
Let's say I have an unordered map defined as this: unordered_map<string, vector<Obj*>> map // I defined some Obj class If I were to add an element into the map, I would have to do something like: pair<string, vector<Obj*>> newEntry(myString, myVector); map.insert(newEntry); But I just want to add an element into...

Using unordered_map allocator's not default constructor

c++,templates,c++11,unordered-map,tr1
I would like to use a different allocator than the default one in unordered_map. For my specific allocator, I want to call another constructor than the default one (I want to pass an int to the constructor). I think the problem is that unordered_map calls the default constructor of my...

Why can't I use pair as key of unordered_set / unordered_map? [duplicate]

c++,dictionary,set,unordered-map,unordered-set
This question already has an answer here: Unordered set of pairs, compilation error 2 answers Both std::set<> and std::map<> can use a std::pair as a key, but why can't std::unordered_set<> and std::unordered_map<>? For example: unordered_set<pair<int,int> > S; S.insert(make_pair(0, 1)); Does not compile....

Accessing the values of map from its key using pointer to map

c++,pointers,hashmap,dynamic-arrays,unordered-map
I want to dynamically allocate an array of pointers to an unordered_map in C++. The std::unordered map has been typedef as 'dictionary'. dict_array= ( dictionary **) calloc(input_size, sizeof(dictionary*)); Now I want to access the individual hashmaps, and for each individual hashmap (mydict), I want to access the values using some...

iterating over unordered_map

c++,c++11,auto,unordered-map
In a for loop with auto, an iterator iterates over an unordered_map. Like this: using RuleIndex = std::unordered_map<uint, Symbol*>; RuleIndex rule_index; for(const auto & rule_pair : rule_index ) { std::cout << rule_pair.first << ": "; printList(rule_pair.second, 0); std::cout << std::endl; } Assume all variables are defined properly, since the code...

C++ unordered_map move beginner error

c++11,move,unordered-map
Sorry for the noob question here, but i don't even know what to search on google. I tried some documention on the move semantic but i still can't solve my problem. So if somebody can give me some clues, i'll be really thanksfull. About the code: graph.hpp: #ifndef GRAPH_HPP #define...

c++ unordered_map for user defined data type

c++,unordered-map
I am writing this code for finding the connected components of a graph. using namespace std; struct node; typedef list<node> AdjList; struct node { bool visited; string name; AdjList adjlist; node(string name1):name(name1),visited(false){} node(const string& a):name(a),visited(false){} }; typedef node node; class graph { int nonodes; unordered_map<string,node> vertices; public: graph(int v):nonodes(v){} void...

How to store more than 2 variables in an unordered_map?

c++,unordered-map
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}}; ...

Efficient way to delete a value from an unordered_map, when given just the value C++

c++,iterator,unordered-map
I have an abstract class called Object and I am using std::unordered_map<int, Object*> objects to contain these Objects within a class called DataSet. Each object has an id associated with it. Normally, when deleting an object from my unordered_map, I can just do iterator = find(id) and then call erase...

C2535 with template-class in unordered_map (Microsoft Visual Studio 2015 CTP6)

c++,hash,unordered-map,visual-studio-2015
I'm getting a strange C2535-compiler error while trying to compile the following code: template<int NUMBER> class Container { public: bool operator==(const Container& other) const { return true; } }; namespace std { template <int NUMBER> class hash<Container<NUMBER>> { public: size_t operator()(const Container<NUMBER> & state) const { return 0; } };...

C++ Unordered Map

c++,dictionary,stl,unordered-map
I have a program in which I am using an iterator to get access to a value of a map entry . #include <iostream> #include <cstdio> #include <unordered_map> #include <utility> #include <algorithm> using namespace std; int main() { int a[]={1,2,4,7,9,3,3,55,66,88,11}; int k = 58; unordered_map<int,int> c; int i=0; for(auto x:...

choice between map or unordered_map for keys consisting of calculated double values.

c++,dictionary,hash,unordered-map,comparison-operators
In order to quickly find coplanar entities in a bunch of planar entities in 3d space, I want to create a mapping from 3d planes to the set of entities lying in that plane (estimated max around ~1000 planes and ~100000 entities) I can create my own custom class to...

Suggestions regarding a string hashing function which ignores ordering of characters

c++,string,hash,unordered-map
I need a hashing function for unordered_map<string, int> in c++. I need the keys to be hashed based on the contents, but it should not depend upon the order of contents. For example, in my map, the keys are string, and I need "ac", "ca" to generate the same hash...

std::hash specialization remains unused by std::unordered_map

c++,templates,c++11,std,unordered-map
I am trying to extend std::hash<T> by supplying a specialization for const char, so that I can use const char* as key type in std::unordered_map. This is what I tried: #include <unordered_map> #include <functional> #include <stdio.h> #include <string.h> #include <ctype.h> namespace std { template<> struct hash<const char*> { size_t operator()(const...

For a C++ unordered_map, how can I add a key-value pair if it's new, and apply a function to the value if it's key already exists?

c++,c++11,stl,unordered-map
Given: unordered_map<string, float> my_map And a pair: auto my_pair = make_pair("some_key", some_float); How can I efficiently insert the pair into the map if some_key does not exist, and if some_key does exist, automatically add some_float to the key's existing value? My current solution (or lack thereof, surely there's a more...

C++ exc_bad_access accessing string field from pointed-to object in unordered_map

c++,vector,exc-bad-access,unordered-map
I'm trying to maintain a fast-access vector in the following manner: MyClass.h: class MyClass{ private: std::vector<Stuff> myStuffList; std::tr1::unordered_map<std::string,Stuff*> myStuffListIndex; ... public: void addToStuffList(std::string key,Stuff stuff); }; MyClass.cpp: ... void MyClass::addToStuffList(std::string name, Stuff stuff){ myStuffList.push_back(stuff);//our man is guaranteed to be at tail myStuffListIndex[name] = &myStuffList[myStuffList.size()-1];//store //pointer to object that we just...

C++: Error outputting a custom hash key value from unordered_map to std::cout

c++,hash,unordered-map,ostream
I am trying to build an std::unordered_map with a custom type as a key. The custom type is a simple std::vector<double>. The idea is that it will function as a convenient container for 2D points on a grid. Everything is working correctly except for outputting the hashed key. Here is...

Trouble creating custom hash function unordered_map?

c++,boost,unordered-map,hash-function
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...

Dereference the first part of the pair object for an unordered map

c++,unordered-map,std-pair
I have a C++ 03 compliant compiler. I am using a pair object when inserting the element into an unordered map using the following code: unordered_map<char, string> mymap; pair<unordered_map<char, string>::iterator,bool> ret; ret = mymap.insert(make_pair('A',"A string of some sort.")); if (ret.second==false) cout << "Could not insert an element into an unordered...

Why does this hashtable function show a compilation error?

c++,c++11,unordered-map
#include <iostream> #include <string> #include <unordered_map> using namespace std; int main (void) { unordered_map<string,string> myhash; int i,n,m,len1,len2; cin>>n>>m; string arr2[3010]; string s1,s2; for ( i = 0; i < m; i++ ) { cin>>s1>>s2; myhash.emplace(s1,s2); } for ( i = 0; i < n; i++ ) { cin>>arr2[i]; } for...

Confusion with std::pairs initialization

c++,qt,hash,unordered-map,std-pair
The code below, when compiled produces the following errors: error C2439: 'std::pair::first' : member could not be initialized error C2440: 'initializing' : cannot convert from 'int' to 'const AnalyticsToolKit::ParcelPassingLogic::ParticipantNode &' When I comment out the bottom line however, there is no such error, so there must be something wrong with...