#include<utility>
#include<alogrithm>
#include<iostream>
using namespace std;
int main(void)
{
int n=5;
int s[]={3,5,1,5,5};
auto r=minmax_element(s,s+n);
cout<<r.second-s<<endl;
cout<<*r.second;
return 0;
}
This is the code to print the maximum element in the array with its index. I want to the maximum element with the least index (the first max in case of a tie)
If you need only the max element, why don't you use the std::max_element()
function? It will do exactly what you want.
auto r = std::max_element(s, s+n);
cout << r-s << endl; // index of the maximum element in [s, s+n)
cout << *r << endl; // value of the maximum element in [s, s+n)