确保当对象更改时多重集重新排序

发布于 2024-09-17 10:35:14 字数 210 浏览 11 评论 0原文

我有一个带有自定义谓词函数的多重集,例如 multiset 其中 MyCompFunc 查看 MyClass 对象上的属性。在应用程序的运行过程中,对象可能会发生变化,从而导致它们重新排序。

发生这种情况时,使多重集重新排序的正确方法是什么?手动排序,还是删除修改的对象,更新它,然后重新插入?

I have a multiset with a custom predicate function, e.g multiset<MyClass *,MyCompFunc> where MyCompFunc looks at an attribute on the MyClass objects. During the progress of the application, the objects might change in a way that should cause them to be reordered.

What's the correct way to get the multiset to become reordered when this happens? Manually sort it, or remove the modified object, update it, and re-insert it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

江南烟雨〆相思醉 2024-09-24 10:35:23

如果要更改对象的部分比较键,请首先从集合中删除该项目,然后执行修改,然后将其添加回集合中。这样您就可以避免破坏集合的不变量,因为集合的设计目的不是在关键更改时重新排序。

If you will be changing part of the comparison key of the object, first remove the item from the set, then perform the modification, then add it back into the set. This way you avoid breaking the invariant of the set, as sets just aren't designed to reorder on a key change.

风吹短裙飘 2024-09-24 10:35:22

不要通过引用访问对象。复制它们,如果密钥发生更改,请重新插入它们。您可以使用观察者来自动执行此操作。

Don't access the objects by reference. Copy them, and re-insert them if their key changed. You might use an observer to automate this.

漫雪独思 2024-09-24 10:35:21

我仍然找不到任何明确说明这一点的内容,但是如果您修改 key[*],那么例如您的比较器不满足 25.3 的要求(严格弱顺序):

comp 产生明确定义的关系
关于确定的等价类
按当量计算

comp 如果它在不同时间为相同输入返回不同值,则不是一个明确定义的关系。

在您的情况下,我认为修改不参与比较的 MyClass 字段是可以的。

有趣的是,23.1.2/2 说,“每个关联容器都在 Key 上进行参数化,并且排序关系 Compare 会在 Key 的元素上产生严格的弱排序(25.3)”。我认为我们可以认为这意味着比较器对作为容器元素的 Key 对象产生严格的弱排序,而不必对所有类型 的对象进行严格的弱排序。关键。例如,如果 Key 是一个指针,那么我很确定编写一个取消引用它的比较器是可以的,前提是您不使用空指针作为键。出于同样的原因,我希望我们可以修改不在容器中的密钥。

[*] 通过“修改”,我的意思是做任何改变使用该键和其他键的比较器结果的事情。当然,在这种情况下,您并没有真正修改键本身(这只是一个指针值),但这就是我所说的。

I still can't find anything which says this explicitly, but if you modify a key[*], then for example your comparator doesn't satisfy the requirements of 25.3 (strict weak order):

comp induces a well-defined relation
on the equivalence classes determined
by equiv

comp isn't a well-defined relation if it returns different values for the same inputs at different times.

In your case I believe that it's fine to modify fields of MyClass which aren't involved in the comparison.

Interestingly, 23.1.2/2 says, "Each associative container is parameterized on Key and an ordering relation Compare that induces a strict weak ordering (25.3) on elements of Key". I think we can take this to mean that the comparator induces a strict weak ordering on Key objects which are elements of the container, not necessarily on all objects of type Key. For instance if the Key is a pointer then I'm pretty sure it's fine to write a comparator that dereferences it, provided you don't use a null pointer as a key. By the same reasoning, I hope we can modify a key which isn't in the container.

[*] by "modify", I mean do anything which changes the results of the comparator with that key and some other key. In this case of course you aren't really modifying the key itself (which is just a pointer value), but that's what I'm calling it.

暖树树初阳… 2024-09-24 10:35:19

通常是删除、更新和重新插入。事实上,任何其他东西至少暂时违反了集合/多重集的主要不变量,这显然不是一件好事。

The usual is to remove, update, and re-insert. Virtually anything else at least temporarily violates the primary invariant of a set/multiset, which clearly isn't a good thing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文