unordered_map报告"向量下标超出范围"使用[]运算符时的断言

时间:2018-02-01 21:22:58

标签: c++ vector std unordered-map

这是一个奇怪的。我有一个结构图的地图。尝试插入一个元素会在std :: vector中给出一个静态断言,它读取"向量下标超出范围"

以下是我的定义:

class A {
  struct tag_info_t {
    uint8_t x, y, z;
  }
  using tag_info_map = std::unordered_map<uint16_T, tag_info_t>;
  std::unordered_map<uint16_t, tag_info_map> id_to_poller_to_info_map;

  void foo(uint16_t, uint16_t, tag_info&);
}

我在一个线程中有一个循环,执行这样的插入:

void A::foo(uint16_t tag, uint16_t poller, tag_info& info ) {
  id_to_poller_to_info_map[tag][poller] = info;
}

有时候(而不是每一次)我都会在这一点上达到这个断言!我喜欢一些信息,指出为什么会发生这种情况。这真的令人沮丧。我正在使用MSVC140(VS2015)。谢谢!

1 个答案:

答案 0 :(得分:1)

因为你提到了线程,并且事实上它只是有时会暗示这是一个并发问题。 unordered_map不是线程安全的,您不能同时从多个线程修改同一个实例。 从多个线程读取它很好。

如前所述,尝试提供一个完整的示例,以便我们更好地了解正在发生的事情。

相关问题