C++ STL unordered_map problems and doubts
after some years in Java and C# now I'm back to C++. Of course my programming style is influenced by those languages and I tend to feel the need of a special component that I used massively: the HASH MAP. In STL there is the hash_map, that GCC says it's deprecated and I should use unordered_map. So I turned to it. I confess I'm not sure of the portability of what I am doing as I had to use a compiler switch to turn on the feature -std=c++0x that is of the upcoming standard. Anyway I'm happy with this. As long as I can't 开发者_如何学Pythonget it working since if I put in my class
std::unordered_map<unsigned int, baseController*> actionControllers;
and in a method:
void baseController::attachActionController(unsigned int *actionArr, int len,
baseController *controller) {
for (int i = 0; i < len; i++){
actionControllers.insert(actionArr[i], controller);
}
}
it comes out with the usual ieroglyphs saying it can't find the insert around... hints?
insert
takes a single argument, which is a key-value pair, of type std::pair<const key_type, mapped_type>
. So you would use it like this:
actionControllers.insert(std::make_pair(actionArr[i], controller));
Just use:
actionControllers[ actionArr[i] ] = controller;
this is the operator overloading java owe you for ages :)
If you have already decided to use (expreimental and not yet ready) C++0x, then you can use such a syntax to insert a key value pair into an unordered_map:
actionControllers.insert({ actionArr[i], controller });
This is supported by gcc 4.4.0
Try:
actionControllers.insert(std::make_pair(actionArr[i], controller));
STL insert
is usually map.insert(PAIR(key, value));
. Maybe that is your problem?
PAIR would be std::unordered_map<unsigned int, baseController*>::value_type
精彩评论