获取受不存在元素限制的 QSet 子集
我需要存储 QSet[QTime] (或类似的)数据。我想做的事情是获取 range 中元素的子集,其中 A 和 B 可能不存在,即
vector[int] 5 7 9 11 13 范围 ( 6, 11) => 7 9 11
有可能吗?也许有更好的方法吗? 感谢您的建议
I need to store QSet[QTime] (or similar) data. The thing that i want to do is to get subset of elements in range , where A and B may not exist i.e.
vector[int] 5 7 9 11 13
range ( 6, 11) => 7 9 11
Is it possible? Maybe there is better way of doing that?
Thanks for advice
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
显而易见的解决方案是
std::set
。它是有序的,并提供lower_bound
和upper_bound
方法。这些不要求集合中存在实际边界。The obvious solution would be
std::set<QTime>
. It is ordered, and offerslower_bound
andupper_bound
methods. These do not require that the actual bounds are present in the set.当然可以编写代码来执行此操作,但 QSet 是一个“无序”集合,没有特别有效的操作来执行您要求的操作。您几乎必须枚举它们并测试每个元素以查看它是否合格:
如果这对于您的目的来说不够快,您将不得不使用不同的数据结构和技术。如果您希望对子集进行排序,那么您需要将其存储在有序集合中,但我不会真正将其称为“子集”,而更像是“子范围”或“切片”。 (计算机科学中的“集合”一词通常意味着元素的顺序对应用程序来说并不重要......但@MSalters 指出
std::set
确实 有订单,这对我来说是新闻!)It's certainly possible to write code to do that, but
QSet
is an "unordered" collection, with no particularly efficient operation for doing what you ask. You pretty much have to enumerate them and test each element to see if it qualifies:If that's not fast enough for your purposes you'll have to use different data structures and techniques. And if you want your subset to be sorted then you'll need to store it in an ordered collection, but then I wouldn't really call it a "subset" but something more like a "subrange" or a "slice". (The word "set" in computer science usually implies that the order of elements is not interesting for the application...but @MSalters has pointed out that
std::set
does have order, which was news to me!)