Reputation: 5723
I am following http://www.cplusplus.com/reference/map/map/map/.
I am trying to build an invert index structure, which is a map that has a 64 bits integer as a key and each key will hold a pointer to a sub-map. A sub-map will contain int int pair. So I got myself writing some samples:
map<unsigned long long int, map<int, int>*> invertID;
int main(int argc, char *argv[]){
map<int,int>* m = new map<int,int>();
m[10] = 1;
invertID[1] = m;
return 0;
}
But here is the trouble, normally, for a non-pointer type map like
std::map<char,int> first;
as described in http://www.cplusplus.com/reference/map/map/map/, I see that we can do
first['a']= 10;
But if we have a pointer type for map, how can we do that? My code above will generate an error complaining
error C2679: binary '=' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion)
Upvotes: 3
Views: 5419
Reputation: 252
Map is a red-black tree where operations so you use that to get best performance for search and insert..etc. You are using map not Multi-map in invertID. Thus when you fetch certain key in most cases I would assume you looking for pair of elements. unless you have a need for second map for whatever reason I am not thinking of now, I would go for this:
std::map < unsigned long long int, std::pair < int , int > > invertID;
int main(){
std::pair<int,int> m;
m = std::make_pair(1,2);
invertID[1] = m;
return 0;
}
Have fun.
Upvotes: 2
Reputation: 227390
You can de-reference the pointer:
(*m)[10] = 1;
or
m->operator[](10) = 1;
On the other hand, do you really need all these pointers? Would your program suffer greatly from using this form?
map<unsigned long long int, map<int, int>> invertID;
int main()
{
map<int,int> m;
invertID[1][10] = 1;
}
Upvotes: 7