Results 1 to 9 of 9

Thread: QMap problem

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QMap problem

    I agree.
    But the OP wrote he wants to insert into the map. You can't do that with value().

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QMap problem

    but we can use insert, since QMap stores unique values.
    it's just IMHO.
    but I preffered this method.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QMap problem

    but can you see the difference:
    Qt Code:
    1. T & operator[] ( const Key & key );
    To copy to clipboard, switch view to plain text mode 
    and
    Qt Code:
    1. const T value ( const Key & key ) const;
    To copy to clipboard, switch view to plain text mode 
    ?
    operator[] is returning reference to the stored item, so you can change it with that reference, but value() is returning const copy of the item, so you can change it, and even if it won't be const it's still a copy so you would be changing the copy only.

    P.S. I copied finctions from docs.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

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

    sophister (8th May 2009)

  5. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QMap problem

    I know about difference.
    using value/insertValue you can achive the same result.
    Qt Code:
    1. ...
    2. QMap<int, QMap<QString,QToolButton> > tbmap;
    3. ...
    4. QMap<QString,QToolButton> map = tbmap.value(1);
    5. map.insertValue("toolButton", new QToolButton);
    6. tmap.insert(1, map);
    To copy to clipboard, switch view to plain text mode 
    the theread starter should remember that if
    map has a value and it is being reset then old value will be overwrote.
    Last edited by spirit; 8th May 2009 at 06:32.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  6. #5
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QMap problem

    this is test of these approaches
    Qt Code:
    1. #include <QMap>
    2. #include <QTime>
    3. #include <QDebug>
    4.  
    5. int main(int argc, char **argv)
    6. {
    7. Q_UNUSED(argc);
    8. Q_UNUSED(argv);
    9.  
    10. QTime startInsertionsTest1;
    11. startInsertionsTest1.start();
    12. qDebug() << "start insertion (insert/value): " << startInsertionsTest1;
    13. QMap<int, QMap<QString, QString> > map1;
    14. for (int i = 0; i < 1000; ++i) {
    15. QMap<QString, QString> tempMap;
    16. for (int j = 0; j < 1000; ++j)
    17. tempMap.insert(QString::number(j), QString::number(j));
    18. map1.insert(i, tempMap);
    19. }
    20. const int test1 = startInsertionsTest1.elapsed();
    21. qDebug() << "end: " << test1;
    22.  
    23. QTime startInsertionsTest2;
    24. startInsertionsTest2.start();
    25. qDebug() << "start insertion (operator []): " << startInsertionsTest2;
    26. QMap<int, QMap<QString, QString> > map2;
    27. for (int i = 0; i < 1000; ++i) {
    28. QMap<QString, QString> tempMap;
    29. for (int j = 0; j < 1000; ++j)
    30. tempMap[QString::number(j)] = QString::number(j);
    31. map2[i] = tempMap;
    32. }
    33. const int test2 = startInsertionsTest2.elapsed();
    34. qDebug() << "end: " << test2;
    35.  
    36. QTime startInsertionsTest3;
    37. startInsertionsTest3.start();
    38. qDebug() << "start updatetion (insert/value): " << startInsertionsTest3;
    39. for (int i = 0; i < 1000; ++i) {
    40. QMap<QString, QString> tempMap(map1.value(i));
    41. QMap<QString, QString>::iterator it(tempMap.begin());
    42. for (; it != tempMap.end(); ++it)
    43. it.value() = "a";
    44. map1.insert(i, tempMap);
    45. }
    46. const int test3 = startInsertionsTest3.elapsed();
    47. qDebug() << "end: " << test3;
    48.  
    49. QTime startInsertionsTest4;
    50. startInsertionsTest4.start();
    51. qDebug() << "start updatetion (operator []): " << startInsertionsTest4;
    52. for (int i = 0; i < 1000; ++i) {
    53. QMap<QString, QString> tempMap(map2[i]);
    54. QMap<QString, QString>::iterator it(tempMap.begin());
    55. for (; it != tempMap.end(); ++it)
    56. it.value() = "a";
    57. map1[i] = tempMap;
    58. }
    59. const int test4 = startInsertionsTest4.elapsed();
    60. qDebug() << "end: " << test4;
    61.  
    62. qDebug() << "--------------------";
    63. qDebug() << "insertion using (insert/value) time: " << test1;
    64. qDebug() << "insertion using (operator[]) time: " << test2;
    65. qDebug() << "insertion diff (insert/value - operator[]): " << test1 - test2;
    66.  
    67. qDebug() << "updation using (insert/value) time: " << test3;
    68. qDebug() << "updation using (operator[]) time: " << test4;
    69. qDebug() << "updation diff (insert/value - operator[]): " << test3 - test4;
    70. return;
    71. }
    To copy to clipboard, switch view to plain text mode 
    this is a result
    start insertion (insert/value): QTime("10:03:22")
    end: 11281
    start insertion (operator []): QTime("10:03:33")
    end: 11578
    start updatetion (insert/value): QTime("10:03:45")
    end: 4047
    start updatetion (operator []): QTime("10:03:49")
    end: 6703
    --------------------
    insertion using (insert/value) time: 11281
    insertion using (operator[]) time: 11578
    insertion diff (insert/value - operator[]): -297
    updation using (insert/value) time: 4047
    updation using (operator[]) time: 6703
    updation diff (insert/value - operator[]): -2656
    PS. maybe it's not so good, but anyway...
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  7. The following user says thank you to spirit for this useful post:

    sophister (8th May 2009)

Similar Threads

  1. Weird problem: multithread QT app kills my linux
    By Ishark in forum Qt Programming
    Replies: 2
    Last Post: 8th August 2008, 09:12
  2. Steps in solving a programming problem?
    By triperzonak in forum General Programming
    Replies: 8
    Last Post: 5th August 2008, 08:47
  3. Qmap problem on a static lib
    By patrik08 in forum Qt Programming
    Replies: 1
    Last Post: 30th March 2007, 12:11
  4. QMap Problem with arguments.
    By ankurjain in forum Qt Programming
    Replies: 1
    Last Post: 24th May 2006, 12:12
  5. QMap destructor bug
    By Ruud in forum Qt Programming
    Replies: 6
    Last Post: 8th April 2006, 09:08

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
  •  
Qt is a trademark of The Qt Company.