C ++函数找到一个双精度数组的最大值? [英] C++ function to find max value in an array of doubles?

查看:409
本文介绍了C ++函数找到一个双精度数组的最大值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常发现自己写的最大值函数通过一个双精度数组搜索我使用这样的功能在图形显示之前标准化数据。

I frequently find myself writing max value functions that search through an array of doubles I use functions like these to normalize data before graphic display.

有更好的方法来找到双精度数组的最大值吗?有一个标准函数来找到数组中的最大值吗?这个操作有什么内在的?我记得专门的ASM指令存在于DSP芯片中。

Is there a better way to find the max value of an array of doubles? Is there a standard function to find the max value in an array? Are there some intrinsic for this operation? I remember specialized ASM instruction existed in DSP chips.

推荐答案

是的!有一个函数 std :: max_element

double arr[LENGTH] = /* ... */
double max = *std::max_element(arr, arr + LENGTH);

您需要 #include< algorithm> 这样做。该标题中有一大堆的好东西,值得花时间来了解更多关于STL容器和算法库,因为它们可以使你的生活更容易。

You'll need to #include <algorithm> to do this. That header has a whole bunch of goodies in it, and it's worth taking the time to learn more about the STL containers and algorithms libraries, as they can make your life so much easier.

只要我们讨论这个问题,考虑看看 std :: vector std :: array 作为原始C ++数组的替换。

As long as we're on the subject, consider looking into std::vector or std::array as replacements for raw C++ arrays. They're safer and a bit easier to use.

希望这有助于!

这篇关于C ++函数找到一个双精度数组的最大值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆