简体   繁体   中英

how to find the value and index of max/min value in C++

I have an array and I want to get the max value and its index. I'm using this code:

#include <iostream>
#include <algorithm>    //max-element
using namespace std;
#define J 5

int main(int argc, char** argv)
...
double ucolumn[J]={};

for(j=0;j<J;j++)
    for(i=0;i<I;i++)
        ucolumn[j]+=u[i][j];

double q=*max_element(ucolumn[0],ucolumn[J]) << endl;
 return 0;
}

but it gives me an error of "illegal indirection", "mismatch in formal parameter list"

您需要将算法与迭代器结合使用,而不要与值结合使用:

std::max_element(ucolumn, ucolumn + J)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM