C++ STL unordered_map问题与疑惑

发布于 2024-08-10 22:36:22 字数 623 浏览 5 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

呢古 2024-08-17 22:36:22

insert 接受一个参数,它是一个键值对,类型为 std::pair 。所以你会这样使用它:

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

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));
仙女 2024-08-17 22:36:22

只需使用:

actionControllers[ actionArr[i] ] = controller;

这是java欠你很久的运算符重载:)

Just use:

actionControllers[ actionArr[i] ] = controller;

this is the operator overloading java owe you for ages :)

冷默言语 2024-08-17 22:36:22

如果您已经决定使用(实验性且尚未准备好)C++0x,那么您可以使用这样的语法将键值对插入到 unordered_map 中:

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

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:

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

This is supported by gcc 4.4.0

自控 2024-08-17 22:36:22

尝试:

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

Try:

actionControllers.insert(std::make_pair(actionArr[i], controller));
靑春怀旧 2024-08-17 22:36:22

STL insert 通常是 map.insert(PAIR(key, value));。也许这就是你的问题?

PAIR 将是 std::unordered_map::value_type

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文