myky
myky

Reputation: 55

Getting subset of QSet limited by non-existent elements

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

Upvotes: 0

Views: 321

Answers (2)

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

Upvotes: 0

MSalters
MSalters

Reputation: 179907

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.

Upvotes: 1

Related Questions