可以重新插入一个元素 "re-validate" 迭代器吗?

Can re-inserting an element "re-validate" iterators?

我在 unordered_set 中有一些对象,在某些时候需要以不改变散列函数或比较运算符行为的方式进行更新。将结构拆分为映射或具有可变字段现在不是选项。我想出了一个解决方案:

struct X { int x, y; };
// operator== and hash defined using only x
std::unordered_set<X> mySet;
// insert a bunch of stuff...
mySet.emplace(1, 2); // y contains 2

// get an iterator and a reference to the element
auto it = mySet.find(X{ 1, 3 }); // (y field here doesn't matter)
const X& ref = *it; // get a pointer to the element
std::cout << ref.y << '\n'; // 2
std::cout << it->y << '\n'; // 2

// now i want to change y to 4
mySet.erase(it);
mySet.emplace_hint(it, 1, 4); // y now contains 4
std::cout << ref.y << '\n'; // 4 or UB?
std::cout << it->y << '\n'; // 4 or UB?

这编译并运行得很好,但我不确定标准是否允许我这样做,因为擦除会使元素被擦除的迭代器失效。使用具有相同值的 insert()(有或没有迭代器提示),是否 保证 新元素将存储在相同的内存位置,因此 "re-validating" 迭代器和参考资料?

一旦迭代器失效;而已。你不能再让它神奇地指向正确的东西了。什么是正确的事情?它之前指向的东西;现在坐在原来位置的东西?

如果你不使迭代器失效;那么你永远不会接触未定义的行为;但是使用无效的迭代器是 UB。

不,这是未定义的行为,它起作用只是运气。

考虑如果 unordered_set 实现在您的 erase 调用后调整容器的大小会发生什么。