STL排序集,其中订单的条件可能改变 [英] STL sorted set where the conditions of order may change

查看:95
本文介绍了STL排序集,其中订单的条件可能改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这个想法是当项目被添加到集合时,它们自然是按我想要的顺序排序



然而,我只是意识到,排序谓词可以随着时间的推移而改变。



因此,两个问题真的是:


  1. 这些项目是否会出现故障是否有害?我可以说,可能发生的最糟糕的事情是,新的条目可能被放在错误的地方(实际上我可以生活)。


  2. 有没有办法刷新集合的排序?你不能在一个集合上使用std :: sort()。


任何一个



感谢,



John

解决方案

set 使用排序查找项目。如果你将根据ordering1插入N个项目,并根据ordering2插入一个项目,那么该集合不能知道该项目是否已经存在。



这将违反类不变



因此 有害。


I have a C++ STL set with a custom ordering defined.

The idea was that when items get added to the set, they're naturally ordered as I want them.

However, what I've just realised is that the ordering predicate can change as time goes by.

Presumably, the items in the set will then no longer be in order.

So two questions really:

  1. Is it harmful that the items would then be out of order? Am I right in saying that the worst that can happen is that new entries may get put into the wrong place (which actually I can live with). Or, could this cause crashes, lost entries etc?

  2. Is there a way to "refresh" the ordering of the set? You can't seem to use std::sort() on a set. The best I can come up with is dumping out the contents to a temp container and re-add them.

Any ideas?

Thanks,

John

解决方案

set uses the ordering to lookup items. If you would insert N items according to ordering1 and insert an item according to ordering2, the set cannot find out if the item is already in.

It will violate the class invariant that every item is in there only once.

So it does harm.

这篇关于STL排序集,其中订单的条件可能改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆