Results 1 to 7 of 7

Thread: How to implement operator<() for QMap

  1. #1
    Join Date
    Sep 2008
    Posts
    93
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default How to implement operator<() for QMap

    I hope to use QMap as template
    QMap<ClassA, ClassB> localMap;

    Qt Code:
    1. class ClassA
    2. {
    3. public:
    4. setIndex(int index);
    5. setFlag(bool flag);
    6. checkIndex();
    7. checkFlag();
    8. private:
    9. int index;
    10. bool flag;
    11. };
    To copy to clipboard, switch view to plain text mode 

    In order to use "Key" in QMap, I have to implement operator<(). But I do not know how to do it. Please help me:

    }
    Last edited by jpn; 8th January 2009 at 13:24. Reason: missing [code] tags

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to implement operator<() for QMap

    There is an example in QMap docs.
    J-P Nurmi

  3. #3
    Join Date
    Sep 2008
    Posts
    93
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: How to implement operator<() for QMap

    I implemented by referring to the example in QMap doc. but it does not work properly. When I try to access the elements in QMap template, there were still some errors caused by operator<()

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

    Default Re: How to implement operator<() for QMap

    so, why don't you show use how you tried to implement a "less than" operation for ClassA?

  5. #5
    Join Date
    Sep 2008
    Posts
    93
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: How to implement operator<() for QMap

    Qt Code:
    1. void ClassA::setKey(int key)
    2. {
    3. index = key;
    4. }
    5.  
    6. void ClassA::setFlag(bool status)
    7. {
    8. flag= status;
    9. }
    10.  
    11. int ClassA::checkKey() const
    12. {
    13. return index;
    14. }
    15.  
    16. bool ClassA::checkFlag() const
    17. {
    18. return flag;
    19. }
    20.  
    21.  
    22. bool ClassA::operator<(const ClassA &i1)
    23. {
    24. if (index != i1.checkKey())
    25. return index < i1.chekcKey();
    26. return false;
    27. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 9th January 2009 at 08:13. Reason: missing [code] tags

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to implement operator<() for QMap

    I'd say the operator has to be const.

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

    Default Re: How to implement operator<() for QMap

    And even if it would not have to be: it should be const.
    Besides, that function is equivalent to:
    Qt Code:
    1. bool ClassA::operator<(const ClassA &i1) const
    2. {
    3. return index < i1.index;
    4. }
    To copy to clipboard, switch view to plain text mode 

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.