PDA

View Full Version : Need help with contrainers



#Dragon
12th January 2016, 19:56
Hi, I've got 3 variables (string, int, int) and I have to put it into contrainer and can get int value by entering key

I used QPair but it not provides to take value by key
I don't know how to explain it, look at my code should be easier



QHash <QString, QVector <QPair <int, int>> > cont;

QString name = "name";
QString name_2 = "name2";

int key = 2;
int key_2 = 5;
int key_3 = 3;

int value = 4;
int value_2 = 0;
int value_3 = 9;

cont[name].push_back(qMakePair(key,value));
cont[name].push_back(qMakePair(key_2,value_2));
cont[name_2].push_back(qMakePair(value_3,key_3));

qDebug() << cont;


qDebug output to see structure


QHash(("name2", QVector(QPair(9,3)))("name", QVector(QPair(2,4), QPair(5,0))))

So I want to get value when I will compare string and put key number
Any idea ?

Regards

d_stranz
13th January 2016, 04:52
QHash does not support multiple entries for the same key, so in your code above, the value inserted in line 14 is erased by the value inserted in line 15.

Another part of your problem is that you are completely mixing up the terms "key" and "value". In your case, the QString is the key, and the QPair<int, int> is the value. If you named your variables properly, you might not get so confused.

anda_skoa
13th January 2016, 11:59
QHash does not support multiple entries for the same key

It does when using insertMulti(), however that is not actually needed here



so in your code above, the value inserted in line 14 is erased by the value inserted in line 15.

No, both lookups return the vector to which each line adds an element.

In any case, it is not at all clear what the problem is.

The debug output shows exactly the content that has been put into the hash.

Cheers,
_

d_stranz
13th January 2016, 17:29
The debug output shows exactly the content that has been put into the hash.


I stand corrected. I shouldn't try to understand code when it is late, I'm sick and doped up on cold medicine.

But as you say, it isn't clear what the problem is.

#Dragon
13th January 2016, 18:15
Another part of your problem is that you are completely mixing up the terms "key" and "value". In your case, the QString is the key, and the QPair<int, int> is the value. If you named your variables properly, you might not get so confused.

Sorry, it was an example code to help explain what I want to reach, I should check it first... my fault.


QHash(("name2", QVector(QPair(9,3)))("name", QVector(QPair(2,4), QPair(5,0))))
QPair(const T1 & value1, const T2 & value2)

Ok, so how can I get value2 form "name" contrainer by entering value1 ?

Thank you for your time spent reading this topic

anda_skoa
13th January 2016, 18:33
What is your reason of having a vector of pairs?
Do you need to keep the order?

Otherwise your use case looks like you'd want a hash or map as the element type of the current hash.

Cheers,
_

#Dragon
13th January 2016, 19:27
No, I don't need to keep the order, it doesn't matter. Just I want to keep file name, attachment number and bad download attempts qty in container, and can easy access/get number of bad download attempts for part of the file (attachment) entering attachment number.

anda_skoa
13th January 2016, 21:43
Then the inner container should be an associative container as well.

Something like


QHash<QString, QHash<int, int> > cont;
cont["name"][2] = 4;
cont["name"][5] = 0;
cont["name2"][9] = 3;

qDebug() << cont["name"][2]; // ==> 4;


Cheers,
_

ChrisW67
13th January 2016, 21:46
Something like


QHash< QString, QHash<int, int> > data;
// add
data[name].insert(part, count);
data[name][part] = count; // maybe this would work too
// retrieve
count = data.value(name).value(part);

If you never need to query for all entries related to a single name then an alternate approach may be a constructed compound key and a single hash


QHash< QString, int > data;
data.insert(QString("%1//%2").arg(name).arg(part), count);
count = data.value( QString("%1//%2").arg(name).arg(part) );

yeye_olive
13th January 2016, 22:01
I second ChrisW67's opinion that a flat hash table with a compound key is better than nested hash tables, unless you need to slice the container along one subkey. There was a discussion on this topic a while ago: http://www.qtcentre.org/threads/64481-Nested-QHash-in-range-for-loop?highlight=nested+qhash.

However I wouldn't serialize pairs of keys to QString; a plain QPair (or std::pair) is all that is needed:

QHash<QPair<QString, int>, int> data;
data.insert(qMakePair(name, part), count);
count = data.value(qMakePair(name, part));

#Dragon
14th January 2016, 19:53
Thank you everybody ! It works very well :)

Cheers