简体   繁体   English

std :: array查找最大值函数

[英]std::array finding max value function

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 ? 是否可以使用更快的方法/其他std / stl函数来查找最大值?

The function std::max returns the greater value between two values. 函数std::max返回两个值之间的较大值。 For a container you can use std::max_element . 对于容器,您可以使用std::max_element Since this returns an iterator to the max element, you need to dereference it. 由于这会将迭代器返回给max元素,因此您需要取消引用。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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