【发布时间】:2018-11-15 15:25:16
【问题描述】:
这是一个带有链接的 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
}
}
}
【问题讨论】:
-
我没有提到它,但很清楚(对我来说我猜)正在使用的链表是 std::list。
标签: c++ c++11 iterator c++-standard-library