QSet in QMap or QHash

I have QMap, and I want to make the QSetkey of this, I could not do this, because it is QSetnot comparable. eg:

QSet<int> intSet;
QMap<QSet<int>, char> charSet;

intSet.insert(1);
intSet.insert(2);
intSet.insert(3);

charSet.insert(intSet, '6');

Is there any way to make it work? and if I inherit from QSetand determine operator <how do I implement it? ie: What should be the logic of comparison?

Note. I really care about performance

+3
source share
4 answers

It seems you know how to make it work: define a function operator<(const QSet<int>&)(I do not believe Qt requires you to subclass QSet, I know that STL does not).

, . , , . , .

: . . -, , . (.. 0-1024 -)? , QByteArray. ....

+5

-,

uint qHash(const QSet<int>& set) {
  uint seed = 0;

  for(int x : set) {
     seed ^= qHash(x) + 0x9e1559a9 + (seed << 6) + (seed >> 2);
  }
  return seed;
}

QMap :

QMap<uint, char> charSet;

uint .

100%, -.

+1

, ( , , ), - .

QSet<int> intSet;
intSet << 1 << 2 << 3 << 3 << 4;

QVector<int> intVector;
intVector.reserve(intSet.size());
qCopy(intSet.begin(), intSet.end(), std::back_inserter(intVector)); // QVector doesn't have a std::vector range constructor atm
qSort(intVector);

QHash<QVector<int>, char> charHash;
charHash[intVector] = '6';

, () .

. , ints, map/hash.

0

, . Wo :

QHash<QSet<int> *, char> charSet;
//then to insert a set
charSet.insert(& intSet, '6');

, , QSet ?

0

All Articles