Re: Search value in QList
Hi, if you only have positive values then use std::min_element.
Ginsengelf
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) :
Code:
double s_value = list.at(0);
int s_index=0;
for(int i=1; i < list.size();i++)
{
if(list.at(i) < s_value)
{
s_value = list.at(i);
s_index = i;
}
}
Re: Search value in QList
Quote:
Originally Posted by
Lesiok
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) :
Code:
double s_value = list.at(0);
int s_index=0;
for(int i=1; i < list.size();i++)
{
if(list.at(i) < s_value)
{
s_value = list.at(i);
s_index = i;
}
}
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:D
Re: Search value in QList
Quote:
Originally Posted by
Ginsengelf
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,
_