[Development] 答复: 答复: [Qt-interest] QMultiHash

Smith Martin Martin.Smith at digia.com
Thu Mar 7 12:57:03 CET 2013

values(key) returns a list of all the values with the key.

remove(key) removes all the values for key.

Then reinsert new values for key.

But why do you want to put the same <key,value> pair multiple times?

From: development-bounces+martin.smith=digia.com at qt-project.org [development-bounces+martin.smith=digia.com at qt-project.org] on behalf of pengliang(彭亮) [pengliang at founder.com]
Sent: Thursday, March 07, 2013 12:51 PM
To: Danny Koppel; pengliang(??); development at qt-project.org
Subject: [Development] 答复: 答复: [Qt-interest] QMultiHash


it allow you to insert some same keys,
i have to find some string whether exist in the hash,
if exist, then set values of hash to 0.

my program is used to remove duplicate rows in two files.
so, i read file1 to QMultiHash<QString,long>hash, qstring are content of rows, long are index of rows,
when i read one row in file2, i have to check whether hash has this row, if yes, i set the values of hash to 0(ignore these rows), else write this row to a new file.

then, create QMultiMap<long,QString>map, insert hash to map, and loop map write QString to another new file.

who has a good advice?


-----Original Message-----
From: Danny Koppel [mailto:D.Koppel at skf-rif.nl]
Sent: 2013-3-7 (星期四) 18:11
To: 'pengliang(??)'; development at qt-project.org
Subject: RE: [Development] 答复:  [Qt-interest] QMultiHash


If you always use replace, there won’t be double keys.
This is depending on your implementation.

Other option:
Use the find() method on the class.
Then  use an iterator on the results to assign the new value.

But if I read your mail, you suggest that you always replace ALL values with the same key.
(Then I would like to know why you have double keys at all?!)
So then I would just use replace. Then the key will occur only once.

I’m working with assumptions, so that’s why I ‘offer’ two solutions.

Kind regards,

Van: development-bounces+d.koppel=skf-rif.nl at qt-project.org [mailto:development-bounces+d.koppel=skf-rif.nl at qt-project.org] Namens pengliang(??)
Verzonden: donderdag 7 maart 2013 10:59
Aan: Danny Koppel; development at qt-project.org
Onderwerp: [Development] 答复: [Qt-interest] QMultiHash

QmultiHash replace() is only replace first item, I want to replace all values when key is matched.
Qt docs : if there are multiple items with the key, the most recently inserted item's value is replaced with value.
Who have a better method?

2.     so I need to use Qmap<long,Qstring>,
I found qt source code below, so I think Qhash<long,Qstring> is ordered by long, am I right?
inline uint qHash(long key) { return qHash(ulong(key)); }
template <class T> inline uint qHash(const T *key)
    return qHash(reinterpret_cast<quintptr>(key));


发件人: Danny Koppel [mailto:D.Koppel at skf-rif.nl]
发送时间: 2013年3月7日 17:39
收件人: 'pengliang(??)'; development at qt-project.org<mailto:development at qt-project.org>
主题: RE: [Development] [Qt-interest] QMultiHash


I don’t know if I understand you correctly but the QMultiHash class has the function replace which does what you describe.
It replaces the value of an existing key or creates a new one if the key doesn’t exist yet.
About point 2: the documentation describes it as follows: “When iterating over a QMap<qthelp://org.qt-project.qtcore.501/qtcore/qmap.html>, the items are always sorted by key. With QHash, the items are arbitrarily ordered.”.

Kind regards,

Van: development-bounces+d.koppel=skf-rif.nl at qt-project.org<mailto:development-bounces+d.koppel=skf-rif.nl at qt-project.org> [mailto:development-bounces+d.koppel=skf-rif.nl at qt-project.org] Namens pengliang(??)
Verzonden: donderdag 7 maart 2013 10:25
Aan: development at qt-project.org<mailto:development at qt-project.org>
Onderwerp: [Development] [Qt-interest] QMultiHash

Hi All

I need to find some key and change their values.
I found If I use QmutableHashIterator<Qstring,int>it , its

findNext<qmutablehashiterator.html#findNext> ( const T & value )

Its parameter is a value, not a key.
So I need to :
If(it.key() == ikey)

I must to check every item? Who have a better method?

2. Qhash:
Some books said Qhash is unorderred?
But I test it.
I insert some string, and loop the hash by QhashIterator, I found the string is orderred.
And I test it Qhash<int,Qstring>, and print key, and found they still orderred.
Why books said it is unorderred?



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/development/attachments/20130307/983df621/attachment.html>

More information about the Development mailing list