【问题标题】:unordered_multimap element insertunordered_multimap 元素插入
【发布时间】:2011-10-10 12:10:40
【问题描述】:

我尝试使用以下代码将一个值插入到 boost unordered_multimap 中,但由于它无法编译,因此没有成功。为什么没有 access [] 运算符?

insert() 方法也不起作用?

#include <iostream>
#include <boost/unordered_map.hpp>

using namespace std;

int main()
{
   typedef boost::unordered_multimap<
      int,
      boost::unordered_multimap<
         int,
         boost::unordered_multimap<int, int>
      >
   > unordered_map;

   unordered_map _3d;

   _3d[0][0][0] = 10;
   _3d[0][0][0].insert(10);

   cout << _3d[0][0][0] << endl;
}

错误:

multimap.cpp||在函数'int main()':
multimap.cpp|19|错误:'operator[]''_3d[0]' 中不匹配
multimap.cpp|21|错误:'operator[]''_3d[0]' 中不匹配
||=== 构建完成:2 个错误,0 个警告 ===

【问题讨论】:

  • 定义“不起作用”。你得到了什么错误?今天我的 ESP 出现故障,所以你必须帮助我来帮助你。
  • 我编辑了上面的错误。
  • std::multimap 也没有 operator[] -- 你为什么期望 unordered_multimap 出现?
  • @ildjarn 我从未尝试过使用 std::multimap。不只是 unordered_multimap 提升吗?
  • @lost_with_coding : unordered_multimap 也在std::tr1 命名空间的TR1 中,在std 命名空间的C++0x 中。在所有情况下,它都是以std::multimap 为模型的,而std::multimap 又没有operator[]——它怎么可能呢?它会返回什么类型?

标签: c++ boost map


【解决方案1】:

unordered_multimapmultimap 都不支持 operator[],因为它的含义没有明确定义。 operator[] 背后的想法是,您可以编写 myMap[key] 来表示“myMap 中与 key 关联的 唯一 值”。在处理unordered_multimap 时,这在数学上没有明确定义;多重映射中可能有许多具有相同键的键值对,并且不清楚您希望拥有哪个特定值。

从您给出的示例来看,您似乎真的想要一个标准的unordered_map 而不是unordered_multimap。如果您尝试使用地图的地图来表示一些更高维的数组,那么您可能只想将每个坐标映射到一个特定的子空间。用unordered_map 替换unordered_multimap 应该可以解决这个问题。

希望这会有所帮助!

【讨论】:

    【解决方案2】:

    boost::unordered_map doesn't appear to have operator[]。请改用查找函数。

    您使用的是什么参考?

    【讨论】:

    • 无论我在谷歌上找到什么,都不算多。我通常会尝试寻找示例,但似乎没有人使用这些东西,或者互联网上有一个秘密社团在示例的“产品”中透明且不透明。
    • @lost_with_coding:使用文档。
    猜你喜欢
    • 1970-01-01
    • 2018-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-06
    • 1970-01-01
    • 2014-01-17
    • 2011-06-16
    相关资源
    最近更新 更多