开发者

Mapping between sets of integers

I need to map a set of integers into sets of integers.

First off, I want to point out that not all possible sets of integers are taken into account. Rather, I create the relevant sets that the application will (ever) use - programmatically, only once, and serialize it in a binary file.

Next, I'd construct a QMap<QSet<int>, QSet<int> > setMap.

Later on, the application constructs another set of integers (by user input), call it userSet and quickly gets setMap[userSet].

Now, the problem is, QMap needs operator < defined for key types, and obv开发者_StackOverflow社区iously QSet<int> doesn't have one.

What can I do about it?


If the sets are static, load them into a table (QVector<QSet<int> >), and use the indexes of the sets in that table as keys and values in the map (QMap<int,int>) instead of the sets themselves.


You need to provide your own overloaded < operator. You can refer to the Employee class example in the QMap documentation. In your case, you need to provide a function like this:

 bool operator<(const QSet<int> &first, const QSet<int> &second)
 {
    // your logic to compare the two sets
 }


You always have the possibility of making the key of the map a QString. Order the numbers in the set, print them somehow (with an arbitrary separator), and be happy.


I'd convert the sets to ordered lists and map those (and then convert back to sets). It's easier to define a comparison operator for an ordered list than for a set. Of course, this may be too slow for you.

You can also use a hash map.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜