获取受不存在元素限制的 QSet 子集

发布于 2024-12-11 16:56:19 字数 161 浏览 0 评论 0原文

我需要存储 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 技术交流群。

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

发布评论

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

评论(2

青春有你 2024-12-18 16:56:19

显而易见的解决方案是 std::set。它是有序的,并提供 lower_boundupper_bound 方法。这些不要求集合中存在实际边界。

The obvious solution would be std::set<QTime>. It is ordered, and offers lower_bound and upper_bound methods. These do not require that the actual bounds are present in the set.

生活了然无味 2024-12-18 16:56:19

当然可以编写代码来执行此操作,但 QSet 是一个“无序”集合,没有特别有效的操作来执行您要求的操作。您几乎必须枚举它们并测试每个元素以查看它是否合格:

QSet<QTime> set;
QTime earliest = QTime::fromString("1.30", "m.s");
QTime latest = QTime::fromString("10.30", "m.s");
...
QSet<QTime> subset;
QSetIterator<QTime> i (set);
while (i.hasNext()) {
    QTime t = i.next();
    if ((t >= earliest) && (t <= latest)) {
        subset.insert(t);
    }
}

如果这对于您的目的来说不够快,您将不得不使用不同的数据结构和技术。如果您希望对子集进行排序,那么您需要将其存储在有序集合中,但我不会真正将其称为“子集”,而更像是“子范围”或“切片”。 (计算机科学中的“集合”一词通常意味着元素的顺序对应用程序来说并不重要......但@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:

QSet<QTime> set;
QTime earliest = QTime::fromString("1.30", "m.s");
QTime latest = QTime::fromString("10.30", "m.s");
...
QSet<QTime> subset;
QSetIterator<QTime> i (set);
while (i.hasNext()) {
    QTime t = i.next();
    if ((t >= earliest) && (t <= latest)) {
        subset.insert(t);
    }
}

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!)

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