- 2
Minimum values of input numbers
How do I find minimum values of input numbers in C++ ?
1 Respuesta
+ 3
The basic approach would be to store the first value in a variable, then iterate over the rest of the collection and check if the current value is less than what is stored in the variable, updating it if that is the case.
A higher-level approach would be to include the <algorithm> header and call std::min_element for your collection:
https://en.cppreference.com/w/cpp/algorithm/min_element
For example, the following would print the minimum value
std::cout << *std::min_element( std::cbegin( arr ), std::cend( arr ) );
where "arr" is your collection.
If you need more than one minimum value, it might be better to sort the collection instead via std::sort():
https://en.cppreference.com/w/cpp/algorithm/sort