How to get the min and the max of a QList in Qt without using any iterator?

You can sort your list and take the first and last elements as min and max correspondingly:

qSort(listVal.begin(), listVal.end());
double min = listVal.first();
double max = listVal.last();

If you don't want iterator as result but directly the value, you may deference the result directly:

//assert(!listVal.empty());
double min = *std::min_element(listVal.begin(), listVal.end());
double max = *std::max_element(listVal.begin(), listVal.end());

And in C++17, with structure binding:

//assert(!listVal.empty());
auto [min, max] = *std::minmax_element(listVal.begin(), listVal.end());

avoiding a round trip of the container:

QList<int> l {2,34,5,2};
auto mm = std::minmax_element(l.begin(), l.end());
qDebug() << *mm.first << *mm.second;

Tags:

C++

Qt

Qlist