Results 1 to 5 of 5

Thread: Search value in QList

  1. #1
    Join Date
    Feb 2019
    Posts
    2
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Search value in QList

    Hi everyone

    I'm programming in qt, with qt 5 and i have a dude. I have a QList<double> list; and this list list contain much values.
    Example:
    Qt Code:
    1. (7.41043, 7.6002, 7.41904, 7.41041, 7.59567, 7.80702, 7.9873, 7.80707, 7.78752, 7.967, 7.78653, 7.96694, 7.40598, 7.40597, 7.5947, 7.76546, 7.94506, 7.82004, 8.0137, 7.99947)
    To copy to clipboard, switch view to plain text mode 

    How can search in the list the value most close to 0 and return this? and know the position in the list?
    Last edited by pelaoqlo; 6th February 2019 at 17:05.

  2. #2
    Join Date
    Jul 2008
    Posts
    409
    Thanks
    9
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Search value in QList

    Hi, if you only have positive values then use std::min_element.

    Ginsengelf

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

    pelaoqlo (6th February 2019)

  4. #3
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,461
    Thanked 271 Times in 266 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Search value in QList

    1. This is QList<double> not QList<int>.
    2. If you can not sort the list then you have to check each element and choose the one for which abs (value) is the smallest. If you only have positive values, you choose the smallest value (you do not need abs). Something like this (for positive only value) :
    Qt Code:
    1. double s_value = list.at(0);
    2. int s_index=0;
    3. for(int i=1; i < list.size();i++)
    4. {
    5. if(list.at(i) < s_value)
    6. {
    7. s_value = list.at(i);
    8. s_index = i;
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to Lesiok for this useful post:

    pelaoqlo (6th February 2019)

  6. #4
    Join Date
    Feb 2019
    Posts
    2
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Search value in QList

    Quote Originally Posted by Lesiok View Post
    1. This is QList<double> not QList<int>.
    2. If you can not sort the list then you have to check each element and choose the one for which abs (value) is the smallest. If you only have positive values, you choose the smallest value (you do not need abs). Something like this (for positive only value) :
    Qt Code:
    1. double s_value = list.at(0);
    2. int s_index=0;
    3. for(int i=1; i < list.size();i++)
    4. {
    5. if(list.at(i) < s_value)
    6. {
    7. s_value = list.at(i);
    8. s_index = i;
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 
    1. Typing error, sorry.
    2. It's work, thanks. I had the doubt of how to get the values from the list, do not observe the method const T &QList::at(int i) const at the documentation, my mistake. Thank you very much for the reply
    Last edited by pelaoqlo; 6th February 2019 at 18:37.

  7. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Search value in QList

    Quote Originally Posted by Ginsengelf View Post
    Hi, if you only have positive values then use std::min_element.
    Yes, definitely std::min_element.

    If there are negative values then with a compare function that takes the abs() for comparison

    Cheers,
    _

Similar Threads

  1. Replies: 1
    Last Post: 14th November 2012, 21:00
  2. Qlist<QLabel *> in Qlist<QAction*>
    By Naahmi in forum Qt Programming
    Replies: 1
    Last Post: 9th September 2011, 08:53
  3. Fast QList search/access
    By tryinghard in forum Newbie
    Replies: 4
    Last Post: 15th January 2011, 02:00
  4. Replies: 4
    Last Post: 20th August 2010, 13:54
  5. BEst way to search QList for item
    By steg90 in forum Qt Programming
    Replies: 11
    Last Post: 18th July 2007, 09:41

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.