Results 1 to 11 of 11

Thread: Need help with contrainers

  1. #1
    Join Date
    Aug 2015
    Location
    Poland/UK
    Posts
    30
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Need help with contrainers

    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

    Qt Code:
    1. QHash <QString, QVector <QPair <int, int>> > cont;
    2.  
    3. QString name = "name";
    4. QString name_2 = "name2";
    5.  
    6. int key = 2;
    7. int key_2 = 5;
    8. int key_3 = 3;
    9.  
    10. int value = 4;
    11. int value_2 = 0;
    12. int value_3 = 9;
    13.  
    14. cont[name].push_back(qMakePair(key,value));
    15. cont[name].push_back(qMakePair(key_2,value_2));
    16. cont[name_2].push_back(qMakePair(value_3,key_3));
    17.  
    18. qDebug() << cont;
    To copy to clipboard, switch view to plain text mode 

    qDebug output to see structure

    Qt Code:
    1. QHash(("name2", QVector(QPair(9,3)))("name", QVector(QPair(2,4), QPair(5,0))))
    To copy to clipboard, switch view to plain text mode 

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

    Regards

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Need help with contrainers

    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.

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Need help with contrainers

    Quote Originally Posted by d_stranz View Post
    QHash does not support multiple entries for the same key
    It does when using insertMulti(), however that is not actually needed here

    Quote Originally Posted by d_stranz View Post
    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,
    _

  4. The following user says thank you to anda_skoa for this useful post:

    d_stranz (13th January 2016)

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Need help with contrainers

    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.

  6. #5
    Join Date
    Aug 2015
    Location
    Poland/UK
    Posts
    30
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Need help with contrainers

    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.

    Qt Code:
    1. QHash(("name2", QVector(QPair(9,3)))("name", QVector(QPair(2,4), QPair(5,0))))
    To copy to clipboard, switch view to plain text mode 
    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

  7. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Need help with contrainers

    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,
    _

  8. #7
    Join Date
    Aug 2015
    Location
    Poland/UK
    Posts
    30
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Need help with contrainers

    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.

  9. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Need help with contrainers

    Then the inner container should be an associative container as well.

    Something like
    Qt Code:
    1. QHash<QString, QHash<int, int> > cont;
    2. cont["name"][2] = 4;
    3. cont["name"][5] = 0;
    4. cont["name2"][9] = 3;
    5.  
    6. qDebug() << cont["name"][2]; // ==> 4;
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  10. The following user says thank you to anda_skoa for this useful post:

    #Dragon (14th January 2016)

  11. #9
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Need help with contrainers

    Something like
    Qt Code:
    1. QHash< QString, QHash<int, int> > data;
    2. // add
    3. data[name].insert(part, count);
    4. data[name][part] = count; // maybe this would work too
    5. // retrieve
    6. count = data.value(name).value(part);
    To copy to clipboard, switch view to plain text mode 
    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
    Qt Code:
    1. QHash< QString, int > data;
    2. data.insert(QString("%1//%2").arg(name).arg(part), count);
    3. count = data.value( QString("%1//%2").arg(name).arg(part) );
    To copy to clipboard, switch view to plain text mode 

  12. The following user says thank you to ChrisW67 for this useful post:

    #Dragon (14th January 2016)

  13. #10
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Need help with contrainers

    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/6448...t=nested+qhash.

    However I wouldn't serialize pairs of keys to QString; a plain QPair (or std::pair) is all that is needed:
    Qt Code:
    1. QHash<QPair<QString, int>, int> data;
    2. data.insert(qMakePair(name, part), count);
    3. count = data.value(qMakePair(name, part));
    To copy to clipboard, switch view to plain text mode 

  14. The following user says thank you to yeye_olive for this useful post:

    #Dragon (14th January 2016)

  15. #11
    Join Date
    Aug 2015
    Location
    Poland/UK
    Posts
    30
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Need help with contrainers

    Thank you everybody ! It works very well

    Cheers

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.