C++ function to find max value in an array of doubles?

后端 未结 1 1866
粉色の甜心
粉色の甜心 2021-01-05 07:14

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.

Is

相关标签:
1条回答
  • 2021-01-05 07:57

    Yep! There's a function called std::max_element:

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

    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.

    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.

    Hope this helps!

    0 讨论(0)
提交回复
热议问题