How to Find Min and Max in a Vector (Qt + STL)

In Qt, finding the minimum or maximum value in a QList or QVector can be frustrating sometimes and it also will complicate things when you just want a quick and simple minimum and maximum. Use the pieces of codes below to get them with a single line of code.

Make sure you add the following to your include list:

#include <algorithm>

It is assumed that you have a vector called vec which contains double values.



Finding the minimum value in a vector of double values or QVector<double>

double min = *std::min_element(vec.constBegin(), vec.constEnd());


Finding the maximum value in a vector of double values or QVector<double>

double max = *std::max_element(vec.constBegin(), vec.constEnd());

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.