Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::array finding max value function

Tags:

c++

arrays

max

here is my array

std::array<double, 64> fm_sim;

I want to find the maximum value in the array.

I can't use

double maxFmSim = std::max(fm_sim.begin(), fm_sim.end());

this is the error :expected an identifier

for now this is what I'm doing

 double maxFmSim = fm_sim[0];
 for (int i = 0; i < 64; i++)
 {
    if(fm_sim[i] > maxFmSim)
    {
        maxFmSim = fm_sim[i];
     }
 }

Is there a faster way/ other std/stl function which I can use in order to find the max value ?

like image 563
Gilad Avatar asked Aug 26 '26 23:08

Gilad


1 Answers

The function std::max returns the greater value between two values. For a container you can use std::max_element. Since this returns an iterator to the max element, you need to dereference it.

double maxFmSim = *std::max_element(fm_sim.begin(), fm_sim.end());
like image 93
Cory Kramer Avatar answered Aug 29 '26 14:08

Cory Kramer