Results 1 to 3 of 3

Thread: [QLinkedList] find and delete item

  1. #1
    Join Date
    Aug 2015
    Posts
    14
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Android
    Thanks
    1

    Default [QLinkedList] find and delete item

    I'm the developer of open source project https://github.com/amreo/gridb
    I want to implement the deSelect function in src/listcoordselecter.cpp

    How to fast way to find and remove item?

    I want to find the iterator and remove the related element like
    Qt Code:
    1. void ListCoordSelecter::deSelect(const Located& loc)
    2. {
    3. QLinkedList<Located>::iterator iter = this->list.begin();
    4.  
    5. while (iter.i->n != NULL)
    6. {
    7. if (iter.reference.x() == loc.x() && iter.reference.y == loc.y())
    8. {
    9. //remove the element pointed by iterator
    10. break; //end of loop
    11. }
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Oct 2009
    Location
    Germany
    Posts
    120
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanked 42 Times in 41 Posts

    Default Re: [QLinkedList] find and delete item

    Hello,

    I'm not clear what is really your question, so here is some guessing.

    1. You use a linked list. For removing a value from the list you first have to find that value. This is O(n) operation for a simple linked list. If that is too slow for your application you should consider using different data structure.

    2. If you have an iterator pointing to the element to be deleted, use
    Qt Code:
    1. iterator QLinkedList::erase(iterator pos)
    To copy to clipboard, switch view to plain text mode 
    to remove the element from the list. It returns an iterator pointing to the element that followed the deleted element.

    3. Your usage of iterators looks a little bit strange to me. I would rewrite it as
    Qt Code:
    1. void ListCoordSelecter::deSelect(const Located& loc)
    2. {
    3. QLinkedList<Located>::iterator iter = list.begin(); // no need for this->
    4.  
    5. while (iter != list.end()) // Usage of NULL to mark the end is implementation detail
    6. {
    7. if (iter->x() == loc.x() && iter->y() == loc.y()) // iter.reference not mentioned in Qt 5.x documentation!
    8. {
    9. //remove the element pointed by iterator
    10. list.erase(iter);
    11. break; //end of loop
    12. }
    13. // don't forget to move the iterator
    14. ++iter;
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 
    Alternatively using C++11 you could also rewrite it to
    Qt Code:
    1. for(auto iter = list.begin(); iter != list.end(); ++iter)
    2. {
    3. if (iter->x() == loc.x() && iter->y() == loc.y()) // iter.reference not mentioned in Qt 5.x documentation!
    4. {
    5. //remove the element pointed by iterator
    6. list.erase(iter);
    7. break; //end of loop
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 

    Best regards
    ars

  3. The following user says thank you to ars for this useful post:

    amreo (31st October 2015)

  4. #3
    Join Date
    Aug 2015
    Posts
    14
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Android
    Thanks
    1

    Default Re: [QLinkedList] find and delete item

    Thanks. I solved the problem

Similar Threads

  1. Get rid empty row in QTreeWidget after item delete
    By shadowCODE in forum Qt Programming
    Replies: 1
    Last Post: 13th October 2014, 10:38
  2. Replies: 1
    Last Post: 29th March 2014, 20:30
  3. Replies: 1
    Last Post: 10th September 2013, 11:07
  4. How to delete item from QListView?
    By damon_1990 in forum Newbie
    Replies: 4
    Last Post: 4th October 2011, 22:47
  5. How to use QMap::remove() to delete some item?
    By jedychen in forum Qt Programming
    Replies: 5
    Last Post: 18th September 2008, 07:29

Tags for this Thread

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.