How to use QHash / MusltiHash - hint needed
-
I am not sure that the OP is actually looking for a multi hash. A multi hash stores any number of entries under the same hash key. In the case that you always want to store exactly 3 integers combine the hash with a tuple:
QHash<QString,std::tuple<int,int,int>> hash;
You can then access the second value withstd::get<1>(hash["key"])
. Notice that in C++ we always start counting from 0. So the second value has index 1.I personally consider using a tuple the quick & dirty approach. If your three integers have a specific purpose it is much better to write a little struct:
struct my_data_t { int a; int b; int c; }; QHash<QString,my_data_t> hash; ... hash["key"].b;
-
@ademmler said in How to use QHash / MusltiHash - hint needed:
Is this possible and if yes - how?
Yes it is. What exactly is the problem?
In documentation you can even find an example for exactly what you're asking: https://doc.qt.io/qt-5/qmultihash.html -
Thx for answering:
You mean this example:
QMultiHash<QString, int> hash1, hash2, hash3; hash1.insert("plenty", 100); hash1.insert("plenty", 2000);
In my example this would be:
QMultiHash<QString, int> hash;
hash.insert("plenty", 100);
hash.insert("plenty", 2000);
hash.insert("plenty", 50);How to access the second value? Is there something like
hash.getValue(2);
I could not find such a routine. It would always need:
QList<int> values = hash.values("plenty"); values.at(2);
-
@ademmler said in How to use QHash / MusltiHash - hint needed:
I could not find such a routine. It would always need:
How should it work otherwise? Since you don't know how many elements you get for one value you must not directly access the third (in your case) element but check the size of the returned container first.
-
I am not sure that the OP is actually looking for a multi hash. A multi hash stores any number of entries under the same hash key. In the case that you always want to store exactly 3 integers combine the hash with a tuple:
QHash<QString,std::tuple<int,int,int>> hash;
You can then access the second value withstd::get<1>(hash["key"])
. Notice that in C++ we always start counting from 0. So the second value has index 1.I personally consider using a tuple the quick & dirty approach. If your three integers have a specific purpose it is much better to write a little struct:
struct my_data_t { int a; int b; int c; }; QHash<QString,my_data_t> hash; ... hash["key"].b;
-
Hi Christian,
It could be a fixed set of entries - which might not be modified.
However @SimonSchroeder gave me a nice idea what to do.thx guys.
-
@ademmler said in How to use QHash / MusltiHash - hint needed:
It could be a fixed set of entries - which might not be modified.
Then a multihash is not the correct container as already pointed out by @SimonSchroeder