Finding minimum value in a vector
I got code here that uses max_element. However, can someone help with a more simplicated version that will produce the same output?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(int)
{
const int len = 3;
const int start = 0;
double arr[len];
arr[0]= 2.4;
arr[1] = 1.2;
arr[2] = 3.67;
double* first = arr;
double* last = arr + len;
//get min
double* val = min_element(first,last);
cout<<"Min val is "<<*val<<endl;
//get max
val = max_element(first,last);
cout<<"Max val is "<<*val<<endl;
return 0;
}
|
You can get rid of lines 7, 8, 14 and 15, then call the algorithms like this:
|
cout<<"Min val is "<< *min_element(arr,arr+len) << endl;
|
Anyway, you are using an array, not a vector
Topic archived. No new replies allowed.