简体   繁体   English

C ++ STL unordered_map问题和疑惑

[英]C++ STL unordered_map problems and doubts

after some years in Java and C# now I'm back to C++. 在Java和C#工作了几年之后,我现在又回到了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. 当然,我的编程风格受这些语言的影响,我倾向于感觉需要大量使用的特殊组件:HASH MAP。 In STL there is the hash_map, that GCC says it's deprecated and I should use unordered_map. 在STL中有hash_map,GCC说它已被弃用,我应该使用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. 我承认我不确定我正在做什么的可移植性,因为我必须使用编译器开关打开即将推出的标准的-std = c ++ 0x功能。 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> . insert接受一个参数,它是一个键值对,类型为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 :) 这是运算符重载java欠你多年:)

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: 如果您已经决定使用(expreimental但尚未准备好)C ++ 0x,那么您可以使用这样的语法将键值对插入到unordered_map中:

  actionControllers.insert({ actionArr[i], controller });

This is supported by gcc 4.4.0 gcc 4.4.0支持此功能

尝试:

actionControllers.insert(std::make_pair(actionArr[i], controller));

STL insert is usually map.insert(PAIR(key, value)); STL insert通常是map.insert(PAIR(key, value)); . Maybe that is your problem? 也许那是你的问题?

PAIR would be std::unordered_map<unsigned int, baseController*>::value_type PAIR将是std::unordered_map<unsigned int, baseController*>::value_type

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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