简体   繁体   中英

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 get 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

尝试:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM