可以重新插入元素“重新验证”迭代器吗?

问题描述 投票:0回答:2

我在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?

这用clang编译并运行得很好,但我不确定标准是否允许我这样做,因为擦除使元素被删除的迭代器无效。使用具有相同值的insert()(有或没有迭代器提示),是否保证新元素将存储在同一内存位置,从而“重新验证”迭代器和引用?

c++ iterator unordered-set
2个回答
3
投票

不,这是未定义的行为,它只是运气,它的工作原理。

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


2
投票

一旦迭代器失效;而已。你无法让它神奇地指向正确的东西。什么是对的?以前指的是事情;那东西现在坐在它的位置?

如果你没有使迭代器无效;然后你永远不会触及未定义的行为;但使用无效的迭代器是UB。

© www.soinside.com 2019 - 2024. All rights reserved.