由 std::find() 返回的迭代器不可取消引用
iterator returned by std::find() is not dereferenceable
这是一个带有链接的HashTable实现的insert((函数。为了避免linked_list重复,如果已经存在值,我会犹豫不决。如果是这样,那么我只是替换现有值,因为它几乎可以在最后评论"更新值"的地方看到。该行发出一个异常,告诉我迭代器不可取消引用。为什么我不能取消引用 std::find(( 返回的迭代器?有没有其他方法可以更新找到的值?
virtual void insert(const K& k, const V& v) {
auto index = hashFctn(k, m_table.capacity());
if (needsToGrow() || m_table[index].m_list.size() >= m_load_factor) {
rehash();
insert(k, v);
}
else {
auto it = std::find(m_table[index].m_list.begin(),
m_table[index].m_list.end(), v);
if (it != m_table[index].m_list.end()) { // if found add it
m_table[index].m_flag = flag::IN_USE;
m_table[index].m_key = k;
m_table[index].m_list.push_back(v);
m_nbrOfElements++;
} else {
*it = v; // update value if exists
}
}
}
你有
if (it != m_table[index].m_list.end()) { // if found add it
// Irrelevant...
} else {
*it = v; // update value if exists
}
如果迭代器it
不是最终迭代器,则执行一些不相关的事情。但在 else 情况下,迭代器it
等于结束迭代器,这是不可取消引用的。然而你取消引用它。
我认为条件应该是相反的,改用==
。
相关文章:
- 为什么范围算法与 std 的迭代器不兼容?
- 矢量迭代器不能与 std::shared_ptr<> 取消引用
- forward_list迭代器不兼容
- 向量迭代器不兼容的错误,用于保存另一个向量的迭代器的向量
- C++,弹出调试断言失败窗口,我得到矢量迭代器不兼容的错误运行时
- f.getline() 迭代器不增加
- 当我发生碰撞检测时,矢量迭代器不兼容
- 为什么C++字符串迭代器不检查错误?
- debug断言矢量迭代器不可用find_if删除
- 为什么标准容器迭代器不会重载"->*"?
- 为什么反向迭代器不是 C++17 标准下的正式迭代器类别?
- C XTREE崩溃在出口上,映射/设置迭代器不可用
- C++:为什么输出迭代器不支持比较操作?
- 迭代器不兼容错误
- C++向量迭代器不是增量的
- 为什么更多的迭代器不是随机访问?
- 为什么迭代器不返回集合的开头
- 字符串迭代器不兼容错误,当我将字符串转换为矢量<byte>时
- 迭代器不能被另一个相同大小的向量重用吗?
- 字符串迭代器不可用C 问题