C++ STL unordered_map问题与疑惑
在 Java 和 C# 工作了几年之后,现在我又回到了 C++。当然,我的编程风格受到这些语言的影响,我倾向于觉得需要一个我大量使用的特殊组件:哈希映射。在STL中有hash_map,GCC说它已被弃用,我应该使用unordered_map。所以我转向它。我承认我不确定我正在做的事情的可移植性,因为我必须使用编译器开关来打开即将推出的标准的功能 -std=c++0x。无论如何我对此很满意。只要我无法让它工作,因为如果我放入我的类
std::unordered_map<unsigned int, baseController*> actionControllers;
和方法中:
void baseController::attachActionController(unsigned int *actionArr, int len,
baseController *controller) {
for (int i = 0; i < len; i++){
actionControllers.insert(actionArr[i], controller);
}
}
它会出现通常的象形文字,说它找不到周围的插入...提示?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
insert
接受一个参数,它是一个键值对,类型为std::pair
。所以你会这样使用它:insert
takes a single argument, which is a key-value pair, of typestd::pair<const key_type, mapped_type>
. So you would use it like this:只需使用:
这是java欠你很久的运算符重载:)
Just use:
this is the operator overloading java owe you for ages :)
如果您已经决定使用(实验性且尚未准备好)C++0x,那么您可以使用这样的语法将键值对插入到 unordered_map 中:
gcc 4.4.0 支持此功能
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:
This is supported by gcc 4.4.0
尝试:
Try:
STL
insert
通常是map.insert(PAIR(key, value));
。也许这就是你的问题?PAIR 将是
std::unordered_map::value_type
STL
insert
is usuallymap.insert(PAIR(key, value));
. Maybe that is your problem?PAIR would be
std::unordered_map<unsigned int, baseController*>::value_type