Why following program is not returning minimum value as 1.
#include <vector>
#include <algorithm>
#include <iostream>
int main ( int argc, char **argv) {
std::vector<int> test;
test.push_back(INT_MAX);
test.push_back(1);
int min = *(std::min(test.begin(), test.end()));
std::cout << "Minimum = " << min << std::endl;
}
It returns minimum
values as 2147483647
You could try this:
int min = *std::min_element(test.begin(), test.end());
std::min
Return the lesser of two arguments Returns the lesser of a and b. If both are equivalent, a is returned.
std::min_element
Returns an iterator pointing to the element with the smallest value in the range [first,last). The comparisons are performed using either operator< for the first version, or comp for the second; An element is the smallest if no other element compares less than it (it may compare equal, though).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With