Results 1 to 6 of 6

Thread: How to use QMap::remove() to delete some item?

  1. #1
    Join Date
    Mar 2007
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question How to use QMap::remove() to delete some item?

    Qt Code:
    1. QMap<int, QString> mapCity;
    2. mapCity.insert(1,"one");
    3. mapCity.insert(2,"two");
    4. ...
    5. mapCity.insert(9,"nine");
    6. ...
    7.  
    8. //Now, I want delete 1,2,3.
    9.  
    10. QMap<int, QString>::Iterator it;
    11. for( it = mapCity.begin(); it != mapCity.end(); ++it)
    12. {
    13. if( it.key() < 4 )
    14. mapCity.remove( it ); //Is that OK??
    15. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 18th September 2008 at 07:32. Reason: missing [code] tags

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to use QMap::remove() to delete some item?

    Remove takes key as argument, so I guess it should be map.remove(it.key());

  3. #3
    Join Date
    Mar 2007
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to use QMap::remove() to delete some item?

    I want ask: use remove() in for(){},maybe wrong, this function will change the iterator's order. So I maybe cannot trival all items!

  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 use QMap::remove() to delete some item?

    Assuming you use Qt4, try QMutableMapIterator instead.
    (With the 'classic' STL style iterator you will get dangling iterators if you remove the item an iterator points to. Java style iterators are easier to use (correctly) imo.)

    HTH

  5. #5
    Join Date
    Mar 2007
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to use QMap::remove() to delete some item?

    unfortunately, I use Qt 3.3.8.I cannot find any functions to get dangling iterators.-_-

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

    Default Re: How to use QMap::remove() to delete some item?

    Qt Code:
    1. QMap<int, QString>::Iterator it;
    2. for( it = mapCity.begin(); it != mapCity.end(); ) // remove increment here
    3. {
    4. if( it.key() < 4 )
    5. mapCity.remove( it++ ); // this is ok; note POSTFIX operator here
    6. else
    7. ++it;
    8. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 18th September 2008 at 07:32. Reason: missing [code] tags

Similar Threads

  1. View, Scene, Item and thread??
    By dungsivn in forum Qt Programming
    Replies: 5
    Last Post: 20th August 2008, 19:21
  2. Item Delegate Painting
    By stevey in forum Qt Programming
    Replies: 3
    Last Post: 9th May 2008, 07:37
  3. Replies: 3
    Last Post: 4th April 2008, 19:51
  4. Replies: 1
    Last Post: 19th April 2007, 22:23
  5. c++, placement delete upon exception
    By stinos in forum General Programming
    Replies: 6
    Last Post: 31st October 2006, 15:38

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.