I want to get a random pair with number n from my QHash.

Now I do it this way:

QHash<QString,QString>::iterator iterator = dictionary.begin();
iterator+= n;
question->setText(iterator.key());

But this seems just ridiculous... There must be a normal way. Can you help me please? I've read the whole entire man-page for QHash already

有帮助吗?

解决方案

QHash doesn't offer random selection. If you have to perform this operation often, then copy (pointers to) the keys() of the hash table into a vector or QVector, get a random index into that and use the key to look up the value in the QHash.

Depending on what else you use the QHash for, you might want to convert it to a vector of pairs at some point and just use that for random selection.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top